﻿using Module_Debug.Hardware;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.IO;
using System.IO.Ports;
using System.Reflection;
using System.Text;
using System.Threading;
using System.Windows.Forms;

namespace Module_Debug
{
    public partial class FT2201 : Form
    {
        byte[] Test1 =
        {
            0x30,0x01,0x02,0xb0,0x00,0x1b,0x3b
        };
        byte[] Test2 =
        {
            0x30,0x01,0x33,0xdf,0x00,0x00,0x58,0x83,0x2d,0x05,
            0x35,0x15,0x01,0x31,0x11,0x0b,0x50,0x00,0x02,0x00,
            0x00,0x43,0x84,0x00,0x10,0x00,0xdf,0xd4,0x0e,0x00,
            0x00,0x13,0xff,0x8c,0x27,0x17,0x0f,0x00,0x55,0x18,
            0x0f,0x00,0x1a,0x0f,0x00,0x1c,0x0f,0x00,0x1e,0x0f,
            0x00,0x00,0x00,0x00,0x82,0xec
        };
        byte[] Test3 =
        {
            0x30,0x01,0x06,0xf3,0x9f,0x00,0x00,0x00,0x02,0x36,
            0xeb
        };
        byte[] Test4 =
        {
            0x30,0x01,0x02,0xf2,0x09,0xe1,0xbc
        };
        byte[] Test5 =
        {
            0x30,0x01,0x02,0x37,0x03,0xa9,0x57
        };
        byte[] Test6 =
        {
            0x30,0x01,0x02,0x14,0xf4,0x65,0x1a
        };
        byte[] Test7 =
        {
            0x30,0x02,0x04,0xf4,0x00,0x00,0x00,0xff,0xb9
        };

        /// <summary>
        /// ///FT2201  
        /// </summary>
        private double B_x = 0.14;
        private double B_y = 0.06;
        private int[] FT2201_GAMMA_GRAY = new int[25];
        int[] GAMMA_FT2201_Point25_Buf = new int[27] { 0, 4, 8, 12, 16, 24, 32, 40, 48, 64, 80, 96, 112, 128, 144, 160, 176, 192, 208, 218, 224, 232, 240, 248, 252, 254, 255 };
        //int[] GAMMA_FT2201_Point10_Buf = new int[11] { 0, 7, 11, 15, 31, 63, 95, 127, 159, 191, 255 };
        int[] GAMMA_FT2201_Point25_BufdataR = new int[27];
        int[] GAMMA_FT2201_Point25_BufdataG = new int[27];
        int[] GAMMA_FT2201_Point25_BufdataB = new int[27];

        //De_Mura
        private bool Listening = false;//是否没有执行完invoke相关操作   
        private bool IClosing = false;//是否正在关闭串口，执行Application.DoEvents，并阻止再次invoke  
        //==============ca310===============
        CaSDK Ca310Obj = new CaSDK();
        private double auto_lum;//
        private double auto_x;
        private double auto_y;
        private double auto_T;
        private double auto_duv;
        private bool ca310calFlag = false;
        private int[] tmp_gamma_195 = new int[93];
        private int[] tmp_gamma_160 = new int[33];
        private int[] tmp_gamma_162 = new int[75];
        private int[] tmp_gamma_120 = new int[78];
        private int[] tmp_gamma_010 = new int[75];
        private int[] tmp_gamma_350 = new int[75];
        private int[] tmp_gamma_300 = new int[75];

        /// <summary>
        /// /auto gamma add 20181031
        /// </summary>
        private int flag1 = 1;
        private int flag2 = 1;
        private int flag3 = 1;
        private Boolean flag_R = true;
        private Boolean flag_G = true;
        private Boolean flag_B = true;


        private ushort[] mygamma_r = new ushort[28];
        private ushort[] mygamma_g = new ushort[28];
        private ushort[] mygamma_b = new ushort[28];


        private int step_h = 12;
        private int step_l = 1;
        //==============ca310===============

        private int CAS200_Measure_Gray = 255;
        private int CAS200_Measure_Count = 0;
        private int CA200_Measure_CE = 0;
        private List<byte> buffer = new List<byte>(2048);//默认分配1/16页内存，并始终限制不允许超过   
        private byte[] Rec_tmp = new byte[1];//
        private byte[] Rec_tmp1 = new byte[1];//
        private byte[] Send_buf = new byte[1];
        //private byte signal_mode;
        public byte start_trans_flag = 0;
        public byte continueflag = 0;
        //public byte RM19X_0xFE_XX = 0;
        //public byte RM300_0XFE_XX = 0;
        public byte RM350_0XFE_XX = 0;
        //public byte RM010_0XFE_XX = 0;
        //public byte CH13721_0XFE_XX = 0;
        //public byte UD61720_0XFE_XX = 0;
        //public byte Seil_port_Find_flag = 0;
        private byte[] Driver_IC = new byte[1];
        //private List<string> ImageType;
        private delegate void MonitorSerialPortChangeEventHandler();
        //private MonitorSerialPortChangeEventHandler MonitorCom;
        //private int serialPortStatus;

        CRC16 CRCData = new CRC16();


        private static bool Delay(int delayTime)
        {
            DateTime now = DateTime.Now;
            int s;
            do
            {
                TimeSpan spand = DateTime.Now - now;
                s = spand.Seconds;
                Application.DoEvents();
            }
            while (s < delayTime);
            return true;
        }


        frmMain MainForm = null;
        public FT2201(frmMain Frm)
        {
            MainForm = Frm;
            InitializeComponent();

        }


        private void Frm_Activated(object sender, EventArgs e)
        {

        }

        //是否已经绑定了事件        
        private bool IsBindEvent(Type type, Control con, string eventName)
        {
            bool isBind = false;

            PropertyInfo pi = type.GetProperty("Events", BindingFlags.Instance | BindingFlags.NonPublic);   //获取type类定义的所有事件的信息
            EventHandlerList ehl = (EventHandlerList)pi.GetValue(con, null);    //获取con对象的事件处理程序列表
            FieldInfo fieldInfo = (typeof(Control)).GetField("EventText", BindingFlags.Static | BindingFlags.NonPublic); //获取Control类Click事件的字段信息
            Delegate d = ehl[fieldInfo.GetValue(null)];
            if (d == null)
            {
                return isBind;
            }
            foreach (Delegate del in d.GetInvocationList())
            {
                if (del.Method.Name == eventName)
                {
                    isBind = true;
                    break;
                }
            }
            return isBind;
        }
        private void asdftest_dfa(object sender, EventArgs e)
        {
            // bool isBind = IsBindEvent(MainForm.comPort.GetType(), MainForm.comPort, "comboBox_TextChanged");
            //为Button1绑定一个事件处理程序
            Button btn = new Button();
            btn.Click += new
            EventHandler(Frm_Activated);
            //获取Button类定义的所有事件的信息
            PropertyInfo pi =
            (typeof(Button)).GetProperty("Events", BindingFlags.Instance |
            BindingFlags.NonPublic);



        }

        //=================================Timer====Start=========================
        private void T1_Tick(object sender, EventArgs e)
        {
            T1.Enabled = false;
            textBox1.Text = textBox1.Text + "通讯故障!\r\n";
            textBox1.Focus();//获取焦点
            textBox1.Select(textBox1.TextLength, 0);//光标定位到文本最后
            textBox1.ScrollToCaret();//滚动到光标处
        }
        private void T2_Tick(object sender, EventArgs e)
        {
            toolStripStatusLabel2.Text = DateTime.Now.ToString();
        }
        //=================================Timer====end=========================
        private void SerialPort1_DataReceived(object sender, System.IO.Ports.SerialDataReceivedEventArgs e)//Getting data from Comm Port
        {
            //if (MainForm.IClosing) return;
            try
            {
                Listening = true;//设置标记，说明我已经开始处理数据，一会儿要使用系统UI的。         
                int n = MainForm.comPort.BytesToRead;
                byte[] buf = new byte[n];
                byte data;

                MainForm.comPort.Read(buf, 0, n);//读取缓冲数据     
                bool data_1_catched = false;//缓存记录数据是否捕获到    
                buffer.AddRange(buf);
                byte CRCH, CRCL;
                if (buffer.Count > 3)
                {
                    if (buffer[0] == 0x0B)          //返回 STM32内部 flash 的配置文件数据
                    {
                        if (buffer.Count >= (Int16)(buffer[2] << 8) + (buffer[3]) + 7)
                        {
                            Rec_tmp = new byte[(Int16)(buffer[2] << 8) + (buffer[3]) + 7];
                            byte[] crcTemp = new byte[(Int16)(buffer[2] << 8) + (buffer[3]) + 7];
                            buffer.CopyTo(0, crcTemp, 0, (Int16)(buffer[2] << 8) + (buffer[3]) + 7);

                            CRCData.CalculateCrc16(crcTemp, out CRCH, out CRCL);
                            if (buffer[(Int16)(buffer[2] << 8) + (buffer[3]) + 7 - 2] != CRCH && buffer[(Int16)(buffer[2] << 8) + (buffer[3]) + 7 - 1] != CRCL) //CRC校验出错
                            {
                                buffer.RemoveRange(0, buffer.Count);
                            }
                            else
                            {
                                buffer.CopyTo(0, Rec_tmp, 0, (Int16)(buffer[2] << 8) + (buffer[3]) + 7);//复制一条完整数据到具体的数据缓存   
                                data_1_catched = true;
                                buffer.RemoveRange(0, (Int16)(buffer[2] << 8) + (buffer[3]) + 7);//正确分析一条数据，从缓存中移除数据。
                            }

                        }
                    }
                    else if (buffer[0] == 0xFE)
                    {
                        Rec_tmp = new byte[buffer[2] + 5];
                        byte[] crcTemp = new byte[buffer[2] + 5];
                        buffer.CopyTo(0, crcTemp, 0, buffer[2] + 5);

                        CRCData.CalculateCrc16(crcTemp, out CRCH, out CRCL);
                        if (buffer[buffer[2] + 5 - 2] != CRCH && buffer[buffer[2] + 5 - 1] != CRCL)     //CRC校验出错
                        {
                            buffer.RemoveRange(0, buffer.Count);
                        }
                        else
                        {
                            buffer.CopyTo(0, Rec_tmp, 0, buffer[2] + 5);//复制一条完整数据到具体的数据缓存   
                            data_1_catched = true;
                            buffer.RemoveRange(0, buffer[2] + 5);//正确分析一条数据，从缓存中移除数据。
                            if (Rec_tmp[4] == 0)
                                continueflag = 1;
                        }
                    }
                    else if (buffer[0] == 0x55)     //STM32 更新bin文件成功  返回
                    {
                        if (buffer.Count >= (buffer[2] + 5))
                        {
                            Rec_tmp = new byte[(buffer[2] + 5)];
                            byte[] crcTemp = new byte[(buffer[2] + 5)];
                            buffer.CopyTo(0, crcTemp, 0, (buffer[2] + 5));

                            CRCData.CalculateCrc16(crcTemp, out CRCH, out CRCL);
                            if (buffer[(buffer[2] + 5) - 2] != CRCH && buffer[(buffer[2] + 5) - 1] != CRCL)       //CRC校验出错
                            {
                                buffer.RemoveRange(0, buffer.Count);
                            }
                            else
                            {
                                buffer.CopyTo(0, Rec_tmp, 0, (buffer[2] + 5));//复制一条完整数据到具体的数据缓存   
                                data_1_catched = true;
                                buffer.RemoveRange(0, (buffer[2] + 5));//正确分析一条数据，从缓存中移除数据。
                                switch (Rec_tmp[3])
                                {
                                    case 0x00:
                                        {
                                            start_trans_flag = 1;
                                        }
                                        break;
                                    case 0x01:
                                        {
                                            if (MainForm.comPort.IsOpen == true) MainForm.comPort.Close();
                                        }
                                        break;
                                    case 0x02:
                                        {
                                            start_trans_flag = 0;
                                        }
                                        break;
                                    default:
                                        break;
                                }
                            }

                        }
                    }
                    else if (((buffer[0] == 0x1E) || (buffer[0] == 0x0E) || (buffer[0] == 0x0D) || (buffer[0] == 0x0A) || (buffer[0] == 0x09) || (buffer[0] == 0x08) || (buffer[0] == 0x19) || (buffer[0] == 0x29) || (buffer[0] == 0x2A) || (buffer[0] == 0x09) || (buffer[0] == 0x2E) || (buffer[0] == 0x2B) || (buffer[0] == 0x2C) || (buffer[0] == 0x2D) || (buffer[0] == 0x2F) || (buffer[0] == 0x30) || (buffer[0] == 0x31) || (buffer[0] == 0x3C) || (buffer[0] == 0x3D) || (buffer[0] == 0x4A)
                        || (buffer[0] == 0x4C) || (buffer[0] == 0x52)) || (buffer[0] == 0x53) && (Setting.TempRecord.cmdLen > 0))
                    {
                        if (buffer.Count >= Setting.TempRecord.cmdLen)
                        {
                            Rec_tmp = new byte[Setting.TempRecord.cmdLen];
                            byte[] crcTemp = new byte[Setting.TempRecord.cmdLen];
                            buffer.CopyTo(0, crcTemp, 0, Setting.TempRecord.cmdLen);

                            CRCData.CalculateCrc16(crcTemp, out CRCH, out CRCL);
                            if (buffer[Setting.TempRecord.cmdLen - 2] != CRCH && buffer[Setting.TempRecord.cmdLen - 1] != CRCL)     //CRC校验出错
                            {
                                buffer.RemoveRange(0, buffer.Count);
                            }
                            else
                            {
                                buffer.CopyTo(0, Rec_tmp, 0, Setting.TempRecord.cmdLen);//复制一条完整数据到具体的数据缓存   
                                data_1_catched = true;
                                buffer.RemoveRange(0, Setting.TempRecord.cmdLen);//正确分析一条数据，从缓存中移除数据。 
                            }
                        }
                    }
                    else
                        buffer.RemoveRange(0, buffer.Count);
                }
                if (data_1_catched)
                {
                    this.Invoke((EventHandler)(delegate
                    {
                        T1.Enabled = false; //停止超时计数

                        //------------------------------------------------------------------------------------------------------------------------

                        ////---------------------------------------------------RM67160/RM67162/RM69310---------------------------------------
                        if ((Rec_tmp[0] == 0x0E) || (Rec_tmp[0] == 0x1E) || (Rec_tmp[0] == 0x52))  //RM67160/RM67162/RM69310
                        {
                            string tmp = "";
                            switch (Rec_tmp[1])
                            {
                                case 0x01:
                                    {
                                        if (Rec_tmp[4] == 0x00)
                                            textBox1.Text = textBox1.Text + "寄存器\t写 OK！\r\n";
                                    }
                                    break;

                                case 0x02:
                                    {

                                        {

                                            tmp = String.Format("{0:X2}", Rec_tmp[4]);//16进制
                                            FT2201_txtRegData.Text = tmp;
                                            textBox1.Text = textBox1.Text + "寄存器\t读 OK！\r\n";
                                        }
                                    }
                                    break;
                                case 0x03://///write gamma ok  RM69310
                                    textBox1.Text = textBox1.Text + "寄gamma存器\t写 OK！\r\n";
                                    break;

                                case 0x04://///read gamma ok  RM69310
                                    {
                                        textBox1.Text = textBox1.Text + "读gamma寄存器\t 读 OK！\r\n";
                                    }
                                    break;

                                case 0x05://///write otp ok  RM69310
                                    textBox1.Text = textBox1.Text + "OTP  \t  写 OK！\r\n";
                                    break;

                                case 0x06://///read otp ok  RM69310
                                    textBox1.Text = textBox1.Text + "OTP  \t  Read OK！\r\n";

                                    tmp = String.Format("{0:X2}", Rec_tmp[3]);//16进制


                                    break;
                            }
                        }
                        else if (Rec_tmp[0] == 0x53)//////FT2201用的。
                        {
                            string tmp = "";
                            switch (Rec_tmp[1])
                            {
                                case 0x01:
                                    {
                                        if (Rec_tmp[4] == 0x00)
                                            textBox1.Text = textBox1.Text + "寄存器\t写 OK！\r\n";
                                    }
                                    break;

                                case 0x02:
                                    {

                                        {

                                            for (int i = 3; i < (Rec_tmp[2] + 2); i++)
                                            {
                                                tmp = tmp + String.Format("{0:X2}", Rec_tmp[i]) + ",";//16进制
                                            }


                                            FT2201_txtRegData.Text = tmp;
                                            textBox1.Text = textBox1.Text + "寄存器\t读 OK！\r\n";
                                        }
                                    }
                                    break;

                                case 0x03://///write gamma ok  RM69310
                                    textBox1.Text = textBox1.Text + "寄gamma存器\t写 OK！\r\n";
                                    break;

                                case 0x05://///write otp ok  RM69310
                                    textBox1.Text = textBox1.Text + "OTP  \t  写 OK！\r\n";
                                    break;

                                default:
                                    {

                                    }
                                    break;
                            }
                        }
                        ////---------------------------------------------------Demura----------------------------------------
                        else if (Rec_tmp[0] == 0x2F)  //Demura
                        {
                            if ((Rec_tmp[7] == 0x2E) || (Rec_tmp[7] == 0x3C))   // 297/298/299  //350
                            {
                                if (Rec_tmp[7] == 0x2E)// 297/298/299
                                {
                                    textBox1.Text = textBox1.Text + "297 298 299\r\n";
                                }
                                else if (Rec_tmp[7] == 0x3C) //350
                                {
                                    textBox1.Text = textBox1.Text + "350\r\n";
                                }
                                switch (Rec_tmp[1])
                                {
                                    case 0x01:
                                        {
                                            textBox1.Text = textBox1.Text + "Demura RAM Enable\r\n";
                                        }
                                        break;
                                    case 0x02:
                                        {
                                            textBox1.Text = textBox1.Text + "Demura RAM Disable\r\n";
                                        }
                                        break;
                                    case 0x03:
                                        {
                                            if (Rec_tmp[7] == 0x2E)// 297/298/299
                                            {
                                                if (Rec_tmp[4] == 0x80)
                                                {
                                                    textBox1.Text = textBox1.Text + "Demura RAM write Flash complete ! \r\n";
                                                }
                                            }
                                            else if (Rec_tmp[7] == 0x3C) ///350
                                            {
                                                if (Rec_tmp[4] == 0x08)
                                                {
                                                    textBox1.Text = textBox1.Text + "Demura RAM write Flash1 complete ! \r\n";
                                                }
                                            }
                                        }
                                        break;
                                    case 0x0A:///350 only
                                        {
                                            if (Rec_tmp[7] == 0x3C) ///350
                                            {
                                                if (Rec_tmp[4] == 0x08)
                                                {
                                                    textBox1.Text = textBox1.Text + "Demura RAM write Flash2 complete ! \r\n";
                                                }
                                            }
                                        }
                                        break;
                                    case 0x04:
                                        {
                                            textBox1.Text = textBox1.Text + "Checksum: " + Rec_tmp[4].ToString("x") + "," + Rec_tmp[5].ToString("x") + "\r\n";
                                        }
                                        break;
                                    case 0x05:
                                        {
                                            if (Rec_tmp[7] == 0x2E)// 297/298/299
                                            {
                                                if (Rec_tmp[4] == 0x80)
                                                {
                                                    textBox1.Text = textBox1.Text + "Flash Erase Success ! \r\n";
                                                }
                                            }
                                            else if (Rec_tmp[7] == 0x3C) ///350
                                            {
                                                if (Rec_tmp[4] == 0x08)
                                                {
                                                    textBox1.Text = textBox1.Text + "Flash Erase Success ! \r\n";
                                                }
                                            }
                                        }
                                        break;
                                    case 0x06:
                                        {
                                            textBox1.Text = textBox1.Text + "flash ID: " + Rec_tmp[4].ToString("x") + "," + Rec_tmp[5].ToString("x") + "," + Rec_tmp[6].ToString("x") + "\r\n";
                                        }
                                        break;
                                    case 0x07:
                                        {
                                            textBox1.Text = textBox1.Text + "Demura function on \r\n";
                                        }
                                        break;
                                    case 0x08:
                                        {
                                            textBox1.Text = textBox1.Text + "Demura function off \r\n";
                                        }
                                        break;
                                    case 0x09:
                                        {
                                            textBox1.Text = textBox1.Text + "Demura OTP complete$ \r\n";
                                        }
                                        break;
                                    default:
                                        break;
                                }
                            }
                        }

                        textBox1.Focus();//获取焦点
                        textBox1.Select(textBox1.TextLength, 0);//光标定位到文本最后
                        textBox1.ScrollToCaret();//滚动到光标处
                        if (textBox1.Lines.Length > 200) textBox1.Text = "";
                    }));
                }
            }
            finally
            {
                Listening = false;

            }

        }


        //=================================LCD cfg function:OLED_Show_Click====Start=================
        private void OLED_Show_Click(object sender, EventArgs e)
        {
            Send_buf = new byte[25];
            Button btnTmp = (Button)sender;
            Setting.TempRecord.cmdLen = 25;
            UInt32 ICS307Temp = 0x00;
            Send_buf[0] = 0x0A; //LCD配置标志
            Send_buf[2] = 0x15; //数据长度
            switch (btnTmp.Name)
            {

                case "btnPatternW":                             //写入Pattern参数
                    {
                        Send_buf[1] = 0x07;
                        Send_buf[3] = byte.Parse(numPatternR.Value.ToString());
                        Send_buf[4] = byte.Parse(numPatternG.Value.ToString());
                        Send_buf[5] = byte.Parse(numPatternB.Value.ToString());
                    }
                    break;

                case "btnred":
                    {
                        Send_buf[1] = 0x07;
                        Send_buf[3] = 0xFF;
                        Send_buf[4] = 0;
                        Send_buf[5] = 0;
                    }
                    break; //R
                case "btngreen":
                    {
                        Send_buf[1] = 0x07;
                        Send_buf[3] = 0;
                        Send_buf[4] = 0xFF;
                        Send_buf[5] = 0;
                    }
                    break; //G
                case "btnblue":
                    {
                        Send_buf[1] = 0x07;
                        Send_buf[3] = 0;
                        Send_buf[4] = 0;
                        Send_buf[5] = 0xFF;
                    }
                    break; //B
                case "btnwhite":
                    {
                        Send_buf[1] = 0x07;
                        Send_buf[3] = 0xFF;
                        Send_buf[4] = 0xFF;
                        Send_buf[5] = 0xFF;
                    }
                    break; //W
                case "btnblack":
                    {
                        Send_buf[1] = 0x07;
                        Send_buf[3] = 0;
                        Send_buf[4] = 0;
                        Send_buf[5] = 0;
                    }
                    break; //BL
                default:
                    break;
            }

            CRCData.CalculateCrc16(Send_buf, out Send_buf[23], out Send_buf[24]);
            try
            {
                MainForm.comPort.Write(Send_buf, 0, 25);
            }
            catch
            {
                MessageBox.Show("串口有误，请检查", "错误");
            }
            Thread.Sleep(800);
        }

        private void numPatternR_ValueChanged(object sender, EventArgs e)
        {
            if (checkBox1.Checked == true)
            {
                numPatternG.Value = numPatternR.Value;
                numPatternB.Value = numPatternG.Value;
            }
        }

        private void checkBox1_CheckedChanged(object sender, EventArgs e)
        {
            if (checkBox1.Checked == true)
            {
                numPatternG.Value = numPatternR.Value;
                numPatternB.Value = numPatternG.Value;
            }
        }


        //------------------------------------------Demura------------------------------------------------




        private int[] Parttern_FT2201 = new int[27] { 255, 254, 252, 248, 240, 232, 224, 218, 208, 192, 176, 160, 144, 128, 112, 96, 80, 64, 48, 40, 32, 24, 16, 12, 8, 4, 0 };
        private int[] Parttern_RM69310 = new int[25] { 255, 239, 223, 207, 191, 175, 159, 143, 127, 111, 95, 79, 63, 55, 47, 39, 35, 31, 27, 23, 19, 15, 11, 7, 0 };
        private int[] Parttern_RM67195 = new int[30] { 255, 254, 252, 248, 244, 236, 228, 220, 212, 204, 196, 188, 180, 164, 148, 132, 116, 100, 84, 68, 52, 44, 36, 28, 20, 16, 12, 8, 4, 3 };
        private void CAS200_Measure_Information(double Dgv_lum, double Dgv_x, double Dgv_y)
        {
            int Row_Accum = 0, Current_Index = 0, Row_flag = 0;

            DataGridViewRow Row1 = new DataGridViewRow();
            Row_Accum = this.CAS200_dataGridView.Rows.Count - 1;
            if (Row_Accum != 0)
            {
                Current_Index = this.CAS200_dataGridView.SelectedCells[0].RowIndex;
                if ((Current_Index != Row_Accum - 1) && (Current_Index != Row_Accum))
                {
                    Row_Accum = Current_Index;
                    Row_flag = 1;
                }
                else
                {
                    this.CAS200_dataGridView.Rows.Add(Row1);
                }
                if ((Row_Accum != 0) && (Row_flag != 1))
                {
                    if (Current_Index != Row_Accum)
                    {
                        this.CAS200_dataGridView.Rows[Row_Accum - 1].Selected = false;
                        this.CAS200_dataGridView.Rows[Row_Accum].Selected = true;
                    }
                }
            }
            else
            {
                this.CAS200_dataGridView.Rows.Add(Row1);
            }
            this.CAS200_dataGridView.Rows[Row_Accum].Cells[0].Value = Row_Accum + 1;//序号
            //Rows[x]为第x行,Cells[x]为第x列
            this.CAS200_dataGridView.Rows[Row_Accum].Cells[1].Value = Dgv_x.ToString("#0.0000");
            this.CAS200_dataGridView.Rows[Row_Accum].Cells[2].Value = Dgv_y.ToString("#0.0000");
            this.CAS200_dataGridView.Rows[Row_Accum].Cells[3].Value = Dgv_lum.ToString("#0.0000");
            if (Row_flag == 1)
            {
                Row_flag = 0;
                Row_Accum = this.CAS200_dataGridView.Rows.Count - 1;
                this.CAS200_dataGridView.Rows[Current_Index].Selected = false;
                this.CAS200_dataGridView.Rows[Row_Accum - 1].Selected = true;
            }
            CAS200_dataGridView.FirstDisplayedScrollingRowIndex = CAS200_dataGridView.RowCount - 1;
        }
        private void Pattern_Write(byte R_value, byte G_value, byte B_value)
        {
            numPatternR.Value = R_value;
            numPatternG.Value = G_value;
            numPatternB.Value = B_value;
            OLED_Show_Click(btnPatternW, null);
        }
        private void T3_Tick(object sender, EventArgs e)
        {

        }

        private void btn_CAS200_Synt_Click(object sender, EventArgs e)
        {
            Button btnTmp = (Button)sender;

            switch (btnTmp.Name)
            {
                case "btn_CAS200_Measure":
                    {
                        Msr_CA310();
                        CAS200_Measure_Information(auto_lum, auto_x, auto_y);
                    }
                    break;
                case "btn_CAS200Save":
                    {
                        SaveFileDialog sf = new SaveFileDialog(); //对话框
                        sf.Filter = @"csv文件(*.csv)|*.csv|所有文件(*.*)|*.*"; //定义保存的文件的类型

                        string str = "序号,x坐标,y坐标,亮度\r\n";
                        if (sf.ShowDialog() == DialogResult.OK) //如果确定保存
                        {
                            if (sf.FileName == "") return; //如果没有输入文件名
                            try
                            {
                                StreamWriter sw = null; //文件流
                                sw = new StreamWriter(sf.FileName, false, Encoding.UTF8);
                                for (int i = 0; i < (this.CAS200_dataGridView.Rows.Count - 1); i++)
                                {
                                    str = str + this.CAS200_dataGridView.Rows[i].Cells[0].Value.ToString() + ","
                                        + this.CAS200_dataGridView.Rows[i].Cells[1].Value.ToString() + ","
                                        + this.CAS200_dataGridView.Rows[i].Cells[2].Value.ToString() + ","
                                        + this.CAS200_dataGridView.Rows[i].Cells[3].Value.ToString() + "\r\n";

                                }
                                sw.WriteLine(str);
                                sw.Dispose(); //释放资源
                                sw.Close(); //关闭数据流
                                MessageBox.Show("CA310数据保存OK！", "系统提示：");
                            }
                            catch
                            {
                                MessageBox.Show("CA310数据保存时发生错误，请再试！", "系统提示：");
                            }
                        }
                    }
                    break;
                case "btn_CAS200_load":
                    {
                        OpenFileDialog sf = new OpenFileDialog();
                        int Line_Count = 0;
                        string strtmp = "";
                        sf.Filter = @"csv文件(*.csv)|*.csv|所有文件(*.*)|*.*"; //定义文件的类型
                        sf.Title = "数据导入";
                        if (sf.ShowDialog() == DialogResult.OK) //如果确定保存
                        {
                            FileStream fs = new FileStream(sf.FileName, FileMode.Open, FileAccess.Read);
                            StreamReader streamReader = new StreamReader(fs, Encoding.GetEncoding("GBK"));
                            streamReader.BaseStream.Seek(0, SeekOrigin.Begin);
                            string strLine = streamReader.ReadLine();
                            while (!string.IsNullOrEmpty(strLine))
                            {
                                strtmp = strtmp + strLine + ",";
                                strLine = streamReader.ReadLine(); //循环读取下一行
                                Line_Count++;
                            }
                            fs.Close(); //关闭流
                            streamReader.Close();
                            string[] arrtmp = strtmp.Split(new Char[] { ',' });
                            for (int j = 1; j < Line_Count - 1; j++)
                            {
                                DataGridViewRow Row1 = new DataGridViewRow();
                                CAS200_dataGridView.Rows.Add(Row1);
                            }
                            try
                            {
                                for (int i = 0; i < Line_Count - 1; i++)//有一行标题
                                {
                                    CAS200_dataGridView.Rows[i].Cells[0].Value = arrtmp[4 * (i + 1) + 0];
                                    CAS200_dataGridView.Rows[i].Cells[1].Value = arrtmp[4 * (i + 1) + 1];
                                    CAS200_dataGridView.Rows[i].Cells[2].Value = arrtmp[4 * (i + 1) + 2];
                                    CAS200_dataGridView.Rows[i].Cells[3].Value = arrtmp[4 * (i + 1) + 3];
                                }
                                MessageBox.Show("CA310数据导入OK！", "提示：");
                            }
                            catch
                            {
                                MessageBox.Show("CA310数据导入时发生错误！", "提示：");
                            }
                        }
                    }
                    break;
                case "btn_CAS200_Clean":
                    {
                        DialogResult dr = MessageBox.Show("确认清空列表数据吗？", "系统提示", MessageBoxButtons.OKCancel);
                        if (dr == DialogResult.OK)
                        {
                            this.CAS200_dataGridView.Rows.Clear();
                        }
                        else {; }
                    }
                    break;
                case "btn_CAS200_Delect":
                    {
                        int Current_Index, Row_Accum;
                        Current_Index = this.CAS200_dataGridView.SelectedCells[0].RowIndex;
                        DataGridViewRow Row1 = CAS200_dataGridView.Rows[Current_Index];
                        this.CAS200_dataGridView.Rows.Remove(Row1);
                        this.CAS200_dataGridView.Rows[Current_Index].Selected = false;
                        Row_Accum = this.CAS200_dataGridView.Rows.Count - 1;
                        for (Current_Index = 0; Current_Index < Row_Accum; Current_Index++)
                        {
                            this.CAS200_dataGridView.Rows[Current_Index].Cells[0].Value = Current_Index + 1;//序号
                        }
                        this.CAS200_dataGridView.Rows[Row_Accum - 1].Selected = true;
                        CAS200_dataGridView.FirstDisplayedScrollingRowIndex = CAS200_dataGridView.RowCount - 1;
                    }
                    break;

                case "btn_CAS200_Stop":
                    {
                        T3.Enabled = false;
                    }
                    break;
                case "btn_CAS200_Start":
                    {
                        string strLine = Convert.ToString(text_CE_data.Text);
                        string[] arr = strLine.Split(','); //以逗号分割，添加到数组
                                                           //  Pattern_Write((byte)(numPatternR.Value), (byte)(numPatternG.Value), (byte)(numPatternB.Value));
                        CAS200_Measure_Count = 0;
                        CAS200_Measure_Gray = 255;
                        CA200_Measure_CE = byte.Parse((Convert.ToInt32(arr[arr.Length - 1], 16).ToString()));
                        T3.Enabled = true;
                        T3.Interval = int.Parse(CAS200_txt1.Text);//延时
                    }
                    break;
                default: break;
            }
        }

        private void btnCal_Click(object sender, EventArgs e)
        {
            if (btnCal_manual.Text == "0Cal")
                btnCal_manual.Text = "Wait";
            ca310info.Visible = true;
            try
            {
                Ca310Obj.CA310_Connect();
                Ca310Obj.CA310_Initial(byte.Parse(MainForm.CA310_Channel.Text));

                btnAutoGammaNT.Enabled = true;
                CAS200_dataGridView.Enabled = true;
                btn_CAS200Save.Enabled = true;
                btn_CAS200_Measure.Enabled = true;
                btn_CAS200_Clean.Enabled = true;
                btn_CAS200_Delect.Enabled = true;
                gBx_CAS200_Auto_Test.Enabled = true;
            }
            catch (Exception EX)
            {
                MessageBox.Show("CA310 Connect Fail!!" + EX.Message);
            }


            btnCal_manual.Text = "0Cal";
            ca310info.Visible = false;
            ca310info.Enabled = true;
        }

        private void Msr_CA310()
        {
            try
            {
                MainForm.Msr_CASDK();
                auto_lum = MainForm.CASDK_LUM;
                auto_x = MainForm.CASDK_x;
                auto_y = MainForm.CASDK_y;
            }
            catch
            {
                MessageBox.Show("请校准Senser！");
            }
        }

        //=================================LCD cfg function: OLED_Show_Click====end=================


        private int adjust_num = 0;
        private string adjust_class = null;
        private double[,] D_lums;//目标值-单色
        private double[] D_lum;//目标值-白色
        private double D_x, D_y;
        //private double lum_off, x_off, y_off;
        private int index = 0;
        //private int flag1 = 1;
        //private int flag2 = 1;
        //private int flag3 = 1;
        private int adjust_Rnum = 0;
        private int adjust_Gnum = 0;
        private int adjust_Bnum = 0;
        private int adjust_l = 0;
        private int adjust_x = 0;
        private int adjust_y = 0;
        //private int step_l = 1;
        //private int step_m = 5;
        //private int step_h = 12;
        Gamma_Addr GAMMAs = new Gamma_Addr();
        private ushort[] gamma_r = new ushort[31];
        private ushort[] gamma_g = new ushort[31];
        private ushort[] gamma_b = new ushort[31];
        private double lum_off, x_off, y_off;
        private double R_x = 0.67;//
        private double R_y = 0.33;
        private double G_x = 0.22;
        private double G_y = 0.71;

        private void btnBack_Click(object sender, EventArgs e)
        {
            MainForm.comPort.DataReceived -= new SerialDataReceivedEventHandler(SerialPort1_DataReceived);//移除本窗口串口处理函数
            MainForm.comPort.DataReceived += new SerialDataReceivedEventHandler(MainForm.SerialPort1_DataReceived);//
            MainForm.Show();
            this.Hide();
        }

        private void rm69350_KeyDown(object sender, KeyEventArgs e)
        {

            //Thread.Sleep(100);
            //if ((e.KeyCode == Keys.N) | (e.KeyCode == Keys.Left))// ;//&& Keys.A == e.KeyCode)
            //{
            //    Demura_Function_ON.PerformClick();
            //}
            //else if ((e.KeyCode == Keys.M) | (e.KeyCode == Keys.Right))// ;//&& Keys.A == e.KeyCode)
            //{
            //    Demura_Function_OFF.PerformClick();
            //}
        }

        private void FT2201_RegWR_Click(object sender, EventArgs e)
        {
            Button btnTmp = (Button)sender;
            Int32 DataNum;
            string strLine = Convert.ToString(FT2201_txtRegData.Text);
            string[] arr = strLine.Split(','); //以逗号分割，添加到数组

            switch (btnTmp.Name)
            {
                case "FT2201_RegW":
                    {
                        Setting.TempRecord.cmdLen = arr.Length + 5;
                        Send_buf = new byte[arr.Length + 5];
                        Send_buf[0] = 0x53;
                        Send_buf[1] = 0x01;
                        Send_buf[2] = byte.Parse(arr.Length.ToString());
                        for (int i = 0; i < arr.Length; i++)
                        {
                            Send_buf[3 + i] = byte.Parse((Convert.ToInt32(arr[i], 16)).ToString());
                        }
                        CRCData.CalculateCrc16(Send_buf, out Send_buf[5 + arr.Length - 2], out Send_buf[5 + arr.Length - 1]);
                        try
                        {
                            MainForm.comPort.Write(Send_buf, 0, 5 + arr.Length);
                        }
                        catch
                        {
                            MessageBox.Show("串口有误，请检查", "错误");
                        }
                    }
                    break;

                case "FT2201_RegR":
                    {
                        Setting.TempRecord.NT37710CmdType = 0x02;
                        try
                        {
                            DataNum = Convert.ToInt32(FT2201_txtDataNum.Text) + 1;
                            Setting.TempRecord.cmdLen = DataNum + 5;
                            Send_buf = new byte[DataNum + 5];
                            Send_buf[0] = 0x53;
                            Send_buf[1] = 0x02;
                            Send_buf[2] = byte.Parse(Convert.ToString(DataNum));
                            Send_buf[3] = byte.Parse((Convert.ToInt32(arr[0], 16)).ToString());
                            CRCData.CalculateCrc16(Send_buf, out Send_buf[5 + DataNum - 2], out Send_buf[5 + DataNum - 1]);
                        }
                        catch
                        {
                            MessageBox.Show("读取长度有误，请检查", "错误");
                            break;
                        }
                        try
                        {
                            MainForm.comPort.Write(Send_buf, 0, 5 + DataNum);
                        }
                        catch
                        {
                            MessageBox.Show("串口有误，请检查", "错误");
                        }
                    }
                    break;

                case "otp":
                    {
                        Setting.TempRecord.cmdLen = arr.Length + 5;
                        Send_buf = new byte[arr.Length + 5];
                        Send_buf[0] = 0x53;
                        Send_buf[1] = 0x05;
                        Send_buf[2] = byte.Parse(arr.Length.ToString());
                        for (int i = 0; i < arr.Length; i++)
                        {
                            Send_buf[3 + i] = byte.Parse((Convert.ToInt32(arr[i], 16)).ToString());
                        }
                        CRCData.CalculateCrc16(Send_buf, out Send_buf[5 + arr.Length - 2], out Send_buf[5 + arr.Length - 1]);
                        try
                        {
                            MainForm.comPort.Write(Send_buf, 0, 5 + arr.Length);
                        }
                        catch
                        {
                            MessageBox.Show("串口有误，请检查", "错误");
                        }
                    }
                    break;

                default: break;
            }
            T1.Enabled = false;
            T1.Interval = 2000;
            T1.Enabled = true;
        }


        private void btnModeSet_Click(object sender, EventArgs e)
        {
            Send_buf = new byte[7];
            Button btnTmp = (Button)sender;
            Setting.TempRecord.cmdLen = 7;
            //Send_buf[0] = 0x0E;///RM67160/67162
            Send_buf[0] = 0x52;///RM69310
            Send_buf[2] = 0x02;



            switch (btnTmp.Name)
            {


                default:
                    break;
            }

            CRCData.CalculateCrc16(Send_buf, out Send_buf[5], out Send_buf[6]);
            try
            {
                MainForm.comPort.Write(Send_buf, 0, 7);
            }
            catch
            {
                MessageBox.Show("串口有误，请检查", "错误");
            }
            T1.Enabled = false;
            T1.Interval = 3000;
            T1.Enabled = true;

        }


        private void FT2201_Gamma_Click(object sender, EventArgs e)
        {
            Button btnTmp = (Button)sender;
            Send_buf = new byte[107 + 5];
            int max_grey_level = 0x3FF;

            Setting.TempRecord.cmdLen = 107 + 5;
            Send_buf[2] = 107;
            Send_buf[3] = 0;
            Send_buf[0] = 0x53; //FT2201

            switch (btnTmp.Name)
            {
                case "FT2201_GammaW_all":
                    {
                        //Setting.TempRecord.RM69310CmdType = 0x03;       //      3
                        Send_buf[1] = 0x03;                             //写Gamma
                                                                        ////R  gamma
                        GAMMA_FT2201_Point25_BufdataR[0] = max_grey_level & (Convert.ToUInt16(FT2201_gammaR0.Value.ToString()));
                        GAMMA_FT2201_Point25_BufdataR[1] = max_grey_level & (Convert.ToUInt16(FT2201_gammaR1.Value.ToString()));
                        GAMMA_FT2201_Point25_BufdataR[2] = max_grey_level & (Convert.ToUInt16(FT2201_gammaR2.Value.ToString()));
                        GAMMA_FT2201_Point25_BufdataR[3] = max_grey_level & (Convert.ToUInt16(FT2201_gammaR3.Value.ToString()));
                        GAMMA_FT2201_Point25_BufdataR[4] = max_grey_level & (Convert.ToUInt16(FT2201_gammaR4.Value.ToString()));
                        GAMMA_FT2201_Point25_BufdataR[5] = max_grey_level & (Convert.ToUInt16(FT2201_gammaR5.Value.ToString()));
                        GAMMA_FT2201_Point25_BufdataR[6] = max_grey_level & (Convert.ToUInt16(FT2201_gammaR6.Value.ToString()));
                        GAMMA_FT2201_Point25_BufdataR[7] = max_grey_level & (Convert.ToUInt16(FT2201_gammaR7.Value.ToString()));
                        GAMMA_FT2201_Point25_BufdataR[8] = max_grey_level & (Convert.ToUInt16(FT2201_gammaR8.Value.ToString()));
                        GAMMA_FT2201_Point25_BufdataR[9] = max_grey_level & (Convert.ToUInt16(FT2201_gammaR9.Value.ToString()));
                        GAMMA_FT2201_Point25_BufdataR[10] = max_grey_level & (Convert.ToUInt16(FT2201_gammaR10.Value.ToString()));
                        GAMMA_FT2201_Point25_BufdataR[11] = max_grey_level & (Convert.ToUInt16(FT2201_gammaR11.Value.ToString()));
                        GAMMA_FT2201_Point25_BufdataR[12] = max_grey_level & (Convert.ToUInt16(FT2201_gammaR12.Value.ToString()));
                        GAMMA_FT2201_Point25_BufdataR[13] = max_grey_level & (Convert.ToUInt16(FT2201_gammaR13.Value.ToString()));
                        GAMMA_FT2201_Point25_BufdataR[14] = max_grey_level & (Convert.ToUInt16(FT2201_gammaR14.Value.ToString()));
                        GAMMA_FT2201_Point25_BufdataR[15] = max_grey_level & (Convert.ToUInt16(FT2201_gammaR15.Value.ToString()));
                        GAMMA_FT2201_Point25_BufdataR[16] = max_grey_level & (Convert.ToUInt16(FT2201_gammaR16.Value.ToString()));
                        GAMMA_FT2201_Point25_BufdataR[17] = max_grey_level & (Convert.ToUInt16(FT2201_gammaR17.Value.ToString()));
                        GAMMA_FT2201_Point25_BufdataR[18] = max_grey_level & (Convert.ToUInt16(FT2201_gammaR18.Value.ToString()));
                        GAMMA_FT2201_Point25_BufdataR[19] = max_grey_level & (Convert.ToUInt16(FT2201_gammaR19.Value.ToString()));
                        GAMMA_FT2201_Point25_BufdataR[20] = max_grey_level & (Convert.ToUInt16(FT2201_gammaR20.Value.ToString()));
                        GAMMA_FT2201_Point25_BufdataR[21] = max_grey_level & (Convert.ToUInt16(FT2201_gammaR21.Value.ToString()));
                        GAMMA_FT2201_Point25_BufdataR[22] = max_grey_level & (Convert.ToUInt16(FT2201_gammaR22.Value.ToString()));
                        GAMMA_FT2201_Point25_BufdataR[23] = max_grey_level & (Convert.ToUInt16(FT2201_gammaR23.Value.ToString()));
                        GAMMA_FT2201_Point25_BufdataR[24] = max_grey_level & (Convert.ToUInt16(FT2201_gammaR24.Value.ToString()));
                        GAMMA_FT2201_Point25_BufdataR[25] = max_grey_level & (Convert.ToUInt16(FT2201_gammaR25.Value.ToString()));
                        GAMMA_FT2201_Point25_BufdataR[26] = max_grey_level & (Convert.ToUInt16(FT2201_gammaR26.Value.ToString()));

                        ////G  gamma
                        GAMMA_FT2201_Point25_BufdataG[0] = max_grey_level & (Convert.ToUInt16(FT2201_gammaG0.Value.ToString()));
                        GAMMA_FT2201_Point25_BufdataG[1] = max_grey_level & (Convert.ToUInt16(FT2201_gammaG1.Value.ToString()));
                        GAMMA_FT2201_Point25_BufdataG[2] = max_grey_level & (Convert.ToUInt16(FT2201_gammaG2.Value.ToString()));
                        GAMMA_FT2201_Point25_BufdataG[3] = max_grey_level & (Convert.ToUInt16(FT2201_gammaG3.Value.ToString()));
                        GAMMA_FT2201_Point25_BufdataG[4] = max_grey_level & (Convert.ToUInt16(FT2201_gammaG4.Value.ToString()));
                        GAMMA_FT2201_Point25_BufdataG[5] = max_grey_level & (Convert.ToUInt16(FT2201_gammaG5.Value.ToString()));
                        GAMMA_FT2201_Point25_BufdataG[6] = max_grey_level & (Convert.ToUInt16(FT2201_gammaG6.Value.ToString()));
                        GAMMA_FT2201_Point25_BufdataG[7] = max_grey_level & (Convert.ToUInt16(FT2201_gammaG7.Value.ToString()));
                        GAMMA_FT2201_Point25_BufdataG[8] = max_grey_level & (Convert.ToUInt16(FT2201_gammaG8.Value.ToString()));
                        GAMMA_FT2201_Point25_BufdataG[9] = max_grey_level & (Convert.ToUInt16(FT2201_gammaG9.Value.ToString()));
                        GAMMA_FT2201_Point25_BufdataG[10] = max_grey_level & (Convert.ToUInt16(FT2201_gammaG10.Value.ToString()));
                        GAMMA_FT2201_Point25_BufdataG[11] = max_grey_level & (Convert.ToUInt16(FT2201_gammaG11.Value.ToString()));
                        GAMMA_FT2201_Point25_BufdataG[12] = max_grey_level & (Convert.ToUInt16(FT2201_gammaG12.Value.ToString()));
                        GAMMA_FT2201_Point25_BufdataG[13] = max_grey_level & (Convert.ToUInt16(FT2201_gammaG13.Value.ToString()));
                        GAMMA_FT2201_Point25_BufdataG[14] = max_grey_level & (Convert.ToUInt16(FT2201_gammaG14.Value.ToString()));
                        GAMMA_FT2201_Point25_BufdataG[15] = max_grey_level & (Convert.ToUInt16(FT2201_gammaG15.Value.ToString()));
                        GAMMA_FT2201_Point25_BufdataG[16] = max_grey_level & (Convert.ToUInt16(FT2201_gammaG16.Value.ToString()));
                        GAMMA_FT2201_Point25_BufdataG[17] = max_grey_level & (Convert.ToUInt16(FT2201_gammaG17.Value.ToString()));
                        GAMMA_FT2201_Point25_BufdataG[18] = max_grey_level & (Convert.ToUInt16(FT2201_gammaG18.Value.ToString()));
                        GAMMA_FT2201_Point25_BufdataG[19] = max_grey_level & (Convert.ToUInt16(FT2201_gammaG19.Value.ToString()));
                        GAMMA_FT2201_Point25_BufdataG[20] = max_grey_level & (Convert.ToUInt16(FT2201_gammaG20.Value.ToString()));
                        GAMMA_FT2201_Point25_BufdataG[21] = max_grey_level & (Convert.ToUInt16(FT2201_gammaG21.Value.ToString()));
                        GAMMA_FT2201_Point25_BufdataG[22] = max_grey_level & (Convert.ToUInt16(FT2201_gammaG22.Value.ToString()));
                        GAMMA_FT2201_Point25_BufdataG[23] = max_grey_level & (Convert.ToUInt16(FT2201_gammaG23.Value.ToString()));
                        GAMMA_FT2201_Point25_BufdataG[24] = max_grey_level & (Convert.ToUInt16(FT2201_gammaG24.Value.ToString()));
                        GAMMA_FT2201_Point25_BufdataG[25] = max_grey_level & (Convert.ToUInt16(FT2201_gammaG25.Value.ToString()));
                        GAMMA_FT2201_Point25_BufdataG[26] = max_grey_level & (Convert.ToUInt16(FT2201_gammaG26.Value.ToString()));

                        ////B  gamma
                        GAMMA_FT2201_Point25_BufdataB[0] = max_grey_level & (Convert.ToUInt16(FT2201_gammaB0.Value.ToString()));
                        GAMMA_FT2201_Point25_BufdataB[1] = max_grey_level & (Convert.ToUInt16(FT2201_gammaB1.Value.ToString()));
                        GAMMA_FT2201_Point25_BufdataB[2] = max_grey_level & (Convert.ToUInt16(FT2201_gammaB2.Value.ToString()));
                        GAMMA_FT2201_Point25_BufdataB[3] = max_grey_level & (Convert.ToUInt16(FT2201_gammaB3.Value.ToString()));
                        GAMMA_FT2201_Point25_BufdataB[4] = max_grey_level & (Convert.ToUInt16(FT2201_gammaB4.Value.ToString()));
                        GAMMA_FT2201_Point25_BufdataB[5] = max_grey_level & (Convert.ToUInt16(FT2201_gammaB5.Value.ToString()));
                        GAMMA_FT2201_Point25_BufdataB[6] = max_grey_level & (Convert.ToUInt16(FT2201_gammaB6.Value.ToString()));
                        GAMMA_FT2201_Point25_BufdataB[7] = max_grey_level & (Convert.ToUInt16(FT2201_gammaB7.Value.ToString()));
                        GAMMA_FT2201_Point25_BufdataB[8] = max_grey_level & (Convert.ToUInt16(FT2201_gammaB8.Value.ToString()));
                        GAMMA_FT2201_Point25_BufdataB[9] = max_grey_level & (Convert.ToUInt16(FT2201_gammaB9.Value.ToString()));
                        GAMMA_FT2201_Point25_BufdataB[10] = max_grey_level & (Convert.ToUInt16(FT2201_gammaB10.Value.ToString()));
                        GAMMA_FT2201_Point25_BufdataB[11] = max_grey_level & (Convert.ToUInt16(FT2201_gammaB11.Value.ToString()));
                        GAMMA_FT2201_Point25_BufdataB[12] = max_grey_level & (Convert.ToUInt16(FT2201_gammaB12.Value.ToString()));
                        GAMMA_FT2201_Point25_BufdataB[13] = max_grey_level & (Convert.ToUInt16(FT2201_gammaB13.Value.ToString()));
                        GAMMA_FT2201_Point25_BufdataB[14] = max_grey_level & (Convert.ToUInt16(FT2201_gammaB14.Value.ToString()));
                        GAMMA_FT2201_Point25_BufdataB[15] = max_grey_level & (Convert.ToUInt16(FT2201_gammaB15.Value.ToString()));
                        GAMMA_FT2201_Point25_BufdataB[16] = max_grey_level & (Convert.ToUInt16(FT2201_gammaB16.Value.ToString()));
                        GAMMA_FT2201_Point25_BufdataB[17] = max_grey_level & (Convert.ToUInt16(FT2201_gammaB17.Value.ToString()));
                        GAMMA_FT2201_Point25_BufdataB[18] = max_grey_level & (Convert.ToUInt16(FT2201_gammaB18.Value.ToString()));
                        GAMMA_FT2201_Point25_BufdataB[19] = max_grey_level & (Convert.ToUInt16(FT2201_gammaB19.Value.ToString()));
                        GAMMA_FT2201_Point25_BufdataB[20] = max_grey_level & (Convert.ToUInt16(FT2201_gammaB20.Value.ToString()));
                        GAMMA_FT2201_Point25_BufdataB[21] = max_grey_level & (Convert.ToUInt16(FT2201_gammaB21.Value.ToString()));
                        GAMMA_FT2201_Point25_BufdataB[22] = max_grey_level & (Convert.ToUInt16(FT2201_gammaB22.Value.ToString()));
                        GAMMA_FT2201_Point25_BufdataB[23] = max_grey_level & (Convert.ToUInt16(FT2201_gammaB23.Value.ToString()));
                        GAMMA_FT2201_Point25_BufdataB[24] = max_grey_level & (Convert.ToUInt16(FT2201_gammaB24.Value.ToString()));
                        GAMMA_FT2201_Point25_BufdataB[25] = max_grey_level & (Convert.ToUInt16(FT2201_gammaB25.Value.ToString()));
                        GAMMA_FT2201_Point25_BufdataB[26] = max_grey_level & (Convert.ToUInt16(FT2201_gammaB26.Value.ToString()));

                        //////R
                        Send_buf[4 + 0x00] = Convert.ToByte(Convert.ToUInt16(GAMMA_FT2201_Point25_BufdataR[0]) & 0xFF);
                        Send_buf[4 + 0x01] = Convert.ToByte(Convert.ToUInt16(GAMMA_FT2201_Point25_BufdataR[1]) & 0xFF);
                        Send_buf[4 + 0x02] = Convert.ToByte(Convert.ToUInt16(GAMMA_FT2201_Point25_BufdataR[2]) & 0xFF);
                        Send_buf[4 + 0x03] = Convert.ToByte(Convert.ToUInt16(GAMMA_FT2201_Point25_BufdataR[3]) & 0xFF);
                        Send_buf[4 + 0x04] = Convert.ToByte(((Convert.ToUInt16(GAMMA_FT2201_Point25_BufdataR[0]) >> 8) << 0)
                            + ((Convert.ToUInt16(GAMMA_FT2201_Point25_BufdataR[1]) >> 8) << 2)
                            + ((Convert.ToUInt16(GAMMA_FT2201_Point25_BufdataR[2]) >> 8) << 4)
                            + ((Convert.ToUInt16(GAMMA_FT2201_Point25_BufdataR[3]) >> 8) << 6));
                        Send_buf[4 + 0x05] = Convert.ToByte(Convert.ToUInt16(GAMMA_FT2201_Point25_BufdataR[4]) & 0xFF);
                        Send_buf[4 + 0x06] = Convert.ToByte(Convert.ToUInt16(GAMMA_FT2201_Point25_BufdataR[5]) & 0xFF);
                        Send_buf[4 + 0x07] = Convert.ToByte(Convert.ToUInt16(GAMMA_FT2201_Point25_BufdataR[6]) & 0xFF);
                        Send_buf[4 + 0x08] = Convert.ToByte(Convert.ToUInt16(GAMMA_FT2201_Point25_BufdataR[7]) & 0xFF);
                        Send_buf[4 + 0x09] = Convert.ToByte(((Convert.ToUInt16(GAMMA_FT2201_Point25_BufdataR[4]) >> 8) << 0)
                            + ((Convert.ToUInt16(GAMMA_FT2201_Point25_BufdataR[5]) >> 8) << 2)
                            + ((Convert.ToUInt16(GAMMA_FT2201_Point25_BufdataR[6]) >> 8) << 4)
                            + ((Convert.ToUInt16(GAMMA_FT2201_Point25_BufdataR[7]) >> 8) << 6));
                        Send_buf[4 + 0x0A] = Convert.ToByte(Convert.ToUInt16(GAMMA_FT2201_Point25_BufdataR[8]) & 0xFF);
                        Send_buf[4 + 0x0B] = Convert.ToByte(Convert.ToUInt16(GAMMA_FT2201_Point25_BufdataR[9]) & 0xFF);
                        Send_buf[4 + 0x0C] = Convert.ToByte(Convert.ToUInt16(GAMMA_FT2201_Point25_BufdataR[10]) & 0xFF);
                        Send_buf[4 + 0x0D] = Convert.ToByte(Convert.ToUInt16(GAMMA_FT2201_Point25_BufdataR[11]) & 0xFF);
                        Send_buf[4 + 0x0E] = Convert.ToByte(((Convert.ToUInt16(GAMMA_FT2201_Point25_BufdataR[8]) >> 8) << 0)
                            + ((Convert.ToUInt16(GAMMA_FT2201_Point25_BufdataR[9]) >> 8) << 2)
                            + ((Convert.ToUInt16(GAMMA_FT2201_Point25_BufdataR[10]) >> 8) << 4)
                            + ((Convert.ToUInt16(GAMMA_FT2201_Point25_BufdataR[11]) >> 8) << 6));
                        Send_buf[4 + 0x0F] = Convert.ToByte(Convert.ToUInt16(GAMMA_FT2201_Point25_BufdataR[12]) & 0xFF);
                        Send_buf[4 + 0x10] = Convert.ToByte(Convert.ToUInt16(GAMMA_FT2201_Point25_BufdataR[13]) & 0xFF);
                        Send_buf[4 + 0x11] = Convert.ToByte(Convert.ToUInt16(GAMMA_FT2201_Point25_BufdataR[14]) & 0xFF);
                        Send_buf[4 + 0x12] = Convert.ToByte(Convert.ToUInt16(GAMMA_FT2201_Point25_BufdataR[15]) & 0xFF);
                        Send_buf[4 + 0x13] = Convert.ToByte(((Convert.ToUInt16(GAMMA_FT2201_Point25_BufdataR[12]) >> 8) << 0)
                            + ((Convert.ToUInt16(GAMMA_FT2201_Point25_BufdataR[13]) >> 8) << 2)
                            + ((Convert.ToUInt16(GAMMA_FT2201_Point25_BufdataR[14]) >> 8) << 4)
                            + ((Convert.ToUInt16(GAMMA_FT2201_Point25_BufdataR[15]) >> 8) << 6));
                        Send_buf[4 + 0x14] = Convert.ToByte(Convert.ToUInt16(GAMMA_FT2201_Point25_BufdataR[16]) & 0xFF);
                        Send_buf[4 + 0x15] = Convert.ToByte(Convert.ToUInt16(GAMMA_FT2201_Point25_BufdataR[17]) & 0xFF);
                        Send_buf[4 + 0x16] = Convert.ToByte(Convert.ToUInt16(GAMMA_FT2201_Point25_BufdataR[18]) & 0xFF);
                        Send_buf[4 + 0x17] = Convert.ToByte(Convert.ToUInt16(GAMMA_FT2201_Point25_BufdataR[19]) & 0xFF);
                        Send_buf[4 + 0x18] = Convert.ToByte(((Convert.ToUInt16(GAMMA_FT2201_Point25_BufdataR[16]) >> 8) << 0)
                            + ((Convert.ToUInt16(GAMMA_FT2201_Point25_BufdataR[17]) >> 8) << 2)
                            + ((Convert.ToUInt16(GAMMA_FT2201_Point25_BufdataR[18]) >> 8) << 4)
                            + ((Convert.ToUInt16(GAMMA_FT2201_Point25_BufdataR[19]) >> 8) << 6));
                        Send_buf[4 + 0x19] = Convert.ToByte(Convert.ToUInt16(GAMMA_FT2201_Point25_BufdataR[20]) & 0xFF);
                        Send_buf[4 + 0x1A] = Convert.ToByte(Convert.ToUInt16(GAMMA_FT2201_Point25_BufdataR[21]) & 0xFF);
                        Send_buf[4 + 0x1B] = Convert.ToByte(Convert.ToUInt16(GAMMA_FT2201_Point25_BufdataR[22]) & 0xFF);
                        Send_buf[4 + 0x1C] = Convert.ToByte(Convert.ToUInt16(GAMMA_FT2201_Point25_BufdataR[23]) & 0xFF);
                        Send_buf[4 + 0x1D] = Convert.ToByte(((Convert.ToUInt16(GAMMA_FT2201_Point25_BufdataR[20]) >> 8) << 0)
                            + ((Convert.ToUInt16(GAMMA_FT2201_Point25_BufdataR[21]) >> 8) << 2)
                            + ((Convert.ToUInt16(GAMMA_FT2201_Point25_BufdataR[22]) >> 8) << 4)
                            + ((Convert.ToUInt16(GAMMA_FT2201_Point25_BufdataR[23]) >> 8) << 6));
                        Send_buf[4 + 0x1E] = Convert.ToByte(Convert.ToUInt16(GAMMA_FT2201_Point25_BufdataR[24]) & 0xFF);
                        Send_buf[4 + 0x1F] = Convert.ToByte(Convert.ToUInt16(GAMMA_FT2201_Point25_BufdataR[25]) & 0xFF);
                        Send_buf[4 + 0x20] = Convert.ToByte(Convert.ToUInt16(GAMMA_FT2201_Point25_BufdataR[26]) & 0xFF);
                        Send_buf[4 + 0x21] = Convert.ToByte(((Convert.ToUInt16(GAMMA_FT2201_Point25_BufdataR[24]) >> 8) << 0)
                            + ((Convert.ToUInt16(GAMMA_FT2201_Point25_BufdataR[25]) >> 8) << 2)
                            + ((Convert.ToUInt16(GAMMA_FT2201_Point25_BufdataR[26]) >> 8) << 4));

                        /////G
                        Send_buf[4 + 0x22 + 0x00] = Convert.ToByte(Convert.ToUInt16(GAMMA_FT2201_Point25_BufdataG[0]) & 0xFF);
                        Send_buf[4 + 0x22 + 0x01] = Convert.ToByte(Convert.ToUInt16(GAMMA_FT2201_Point25_BufdataG[1]) & 0xFF);
                        Send_buf[4 + 0x22 + 0x02] = Convert.ToByte(Convert.ToUInt16(GAMMA_FT2201_Point25_BufdataG[2]) & 0xFF);
                        Send_buf[4 + 0x22 + 0x03] = Convert.ToByte(Convert.ToUInt16(GAMMA_FT2201_Point25_BufdataG[3]) & 0xFF);
                        Send_buf[4 + 0x22 + 0x04] = Convert.ToByte(((Convert.ToUInt16(GAMMA_FT2201_Point25_BufdataG[0]) >> 8) << 0)
                            + ((Convert.ToUInt16(GAMMA_FT2201_Point25_BufdataG[1]) >> 8) << 2)
                            + ((Convert.ToUInt16(GAMMA_FT2201_Point25_BufdataG[2]) >> 8) << 4)
                            + ((Convert.ToUInt16(GAMMA_FT2201_Point25_BufdataG[3]) >> 8) << 6));
                        Send_buf[4 + 0x22 + 0x05] = Convert.ToByte(Convert.ToUInt16(GAMMA_FT2201_Point25_BufdataG[4]) & 0xFF);
                        Send_buf[4 + 0x22 + 0x06] = Convert.ToByte(Convert.ToUInt16(GAMMA_FT2201_Point25_BufdataG[5]) & 0xFF);
                        Send_buf[4 + 0x22 + 0x07] = Convert.ToByte(Convert.ToUInt16(GAMMA_FT2201_Point25_BufdataG[6]) & 0xFF);
                        Send_buf[4 + 0x22 + 0x08] = Convert.ToByte(Convert.ToUInt16(GAMMA_FT2201_Point25_BufdataG[7]) & 0xFF);
                        Send_buf[4 + 0x22 + 0x09] = Convert.ToByte(((Convert.ToUInt16(GAMMA_FT2201_Point25_BufdataG[4]) >> 8) << 0)
                            + ((Convert.ToUInt16(GAMMA_FT2201_Point25_BufdataG[5]) >> 8) << 2)
                            + ((Convert.ToUInt16(GAMMA_FT2201_Point25_BufdataG[6]) >> 8) << 4)
                            + ((Convert.ToUInt16(GAMMA_FT2201_Point25_BufdataG[7]) >> 8) << 6));
                        Send_buf[4 + 0x22 + 0x0A] = Convert.ToByte(Convert.ToUInt16(GAMMA_FT2201_Point25_BufdataG[8]) & 0xFF);
                        Send_buf[4 + 0x22 + 0x0B] = Convert.ToByte(Convert.ToUInt16(GAMMA_FT2201_Point25_BufdataG[9]) & 0xFF);
                        Send_buf[4 + 0x22 + 0x0C] = Convert.ToByte(Convert.ToUInt16(GAMMA_FT2201_Point25_BufdataG[10]) & 0xFF);
                        Send_buf[4 + 0x22 + 0x0D] = Convert.ToByte(Convert.ToUInt16(GAMMA_FT2201_Point25_BufdataG[11]) & 0xFF);
                        Send_buf[4 + 0x22 + 0x0E] = Convert.ToByte(((Convert.ToUInt16(GAMMA_FT2201_Point25_BufdataG[8]) >> 8) << 0)
                            + ((Convert.ToUInt16(GAMMA_FT2201_Point25_BufdataG[9]) >> 8) << 2)
                            + ((Convert.ToUInt16(GAMMA_FT2201_Point25_BufdataG[10]) >> 8) << 4)
                            + ((Convert.ToUInt16(GAMMA_FT2201_Point25_BufdataG[11]) >> 8) << 6));
                        Send_buf[4 + 0x22 + 0x0F] = Convert.ToByte(Convert.ToUInt16(GAMMA_FT2201_Point25_BufdataG[12]) & 0xFF);
                        Send_buf[4 + 0x22 + 0x10] = Convert.ToByte(Convert.ToUInt16(GAMMA_FT2201_Point25_BufdataG[13]) & 0xFF);
                        Send_buf[4 + 0x22 + 0x11] = Convert.ToByte(Convert.ToUInt16(GAMMA_FT2201_Point25_BufdataG[14]) & 0xFF);
                        Send_buf[4 + 0x22 + 0x12] = Convert.ToByte(Convert.ToUInt16(GAMMA_FT2201_Point25_BufdataG[15]) & 0xFF);
                        Send_buf[4 + 0x22 + 0x13] = Convert.ToByte(((Convert.ToUInt16(GAMMA_FT2201_Point25_BufdataG[12]) >> 8) << 0)
                            + ((Convert.ToUInt16(GAMMA_FT2201_Point25_BufdataG[13]) >> 8) << 2)
                            + ((Convert.ToUInt16(GAMMA_FT2201_Point25_BufdataG[14]) >> 8) << 4)
                            + ((Convert.ToUInt16(GAMMA_FT2201_Point25_BufdataG[15]) >> 8) << 6));
                        Send_buf[4 + 0x22 + 0x14] = Convert.ToByte(Convert.ToUInt16(GAMMA_FT2201_Point25_BufdataG[16]) & 0xFF);
                        Send_buf[4 + 0x22 + 0x15] = Convert.ToByte(Convert.ToUInt16(GAMMA_FT2201_Point25_BufdataG[17]) & 0xFF);
                        Send_buf[4 + 0x22 + 0x16] = Convert.ToByte(Convert.ToUInt16(GAMMA_FT2201_Point25_BufdataG[18]) & 0xFF);
                        Send_buf[4 + 0x22 + 0x17] = Convert.ToByte(Convert.ToUInt16(GAMMA_FT2201_Point25_BufdataG[19]) & 0xFF);
                        Send_buf[4 + 0x22 + 0x18] = Convert.ToByte(((Convert.ToUInt16(GAMMA_FT2201_Point25_BufdataG[16]) >> 8) << 0)
                            + ((Convert.ToUInt16(GAMMA_FT2201_Point25_BufdataG[17]) >> 8) << 2)
                            + ((Convert.ToUInt16(GAMMA_FT2201_Point25_BufdataG[18]) >> 8) << 4)
                            + ((Convert.ToUInt16(GAMMA_FT2201_Point25_BufdataG[19]) >> 8) << 6));
                        Send_buf[4 + 0x22 + 0x19] = Convert.ToByte(Convert.ToUInt16(GAMMA_FT2201_Point25_BufdataG[20]) & 0xFF);
                        Send_buf[4 + 0x22 + 0x1A] = Convert.ToByte(Convert.ToUInt16(GAMMA_FT2201_Point25_BufdataG[21]) & 0xFF);
                        Send_buf[4 + 0x22 + 0x1B] = Convert.ToByte(Convert.ToUInt16(GAMMA_FT2201_Point25_BufdataG[22]) & 0xFF);
                        Send_buf[4 + 0x22 + 0x1C] = Convert.ToByte(Convert.ToUInt16(GAMMA_FT2201_Point25_BufdataG[23]) & 0xFF);
                        Send_buf[4 + 0x22 + 0x1D] = Convert.ToByte(((Convert.ToUInt16(GAMMA_FT2201_Point25_BufdataG[20]) >> 8) << 0)
                            + ((Convert.ToUInt16(GAMMA_FT2201_Point25_BufdataG[21]) >> 8) << 2)
                            + ((Convert.ToUInt16(GAMMA_FT2201_Point25_BufdataG[22]) >> 8) << 4)
                            + ((Convert.ToUInt16(GAMMA_FT2201_Point25_BufdataG[23]) >> 8) << 6));
                        Send_buf[4 + 0x22 + 0x1E] = Convert.ToByte(Convert.ToUInt16(GAMMA_FT2201_Point25_BufdataG[24]) & 0xFF);
                        Send_buf[4 + 0x22 + 0x1F] = Convert.ToByte(Convert.ToUInt16(GAMMA_FT2201_Point25_BufdataG[25]) & 0xFF);
                        Send_buf[4 + 0x22 + 0x20] = Convert.ToByte(Convert.ToUInt16(GAMMA_FT2201_Point25_BufdataG[26]) & 0xFF);
                        Send_buf[4 + 0x22 + 0x21] = Convert.ToByte(((Convert.ToUInt16(GAMMA_FT2201_Point25_BufdataG[24]) >> 8) << 0)
                            + ((Convert.ToUInt16(GAMMA_FT2201_Point25_BufdataG[25]) >> 8) << 2)
                            + ((Convert.ToUInt16(GAMMA_FT2201_Point25_BufdataG[26]) >> 8) << 4));
                        //Send_buf[4 + 0x34 + 0x34] = Convert.ToByte(Convert.ToUInt16(GAMMA_FT2201_Point25_BufdataG[24]) & 0xFF);

                        //////B
                        Send_buf[4 + 0x44 + 0x00] = Convert.ToByte(Convert.ToUInt16(GAMMA_FT2201_Point25_BufdataB[0]) & 0xFF);
                        Send_buf[4 + 0x44 + 0x01] = Convert.ToByte(Convert.ToUInt16(GAMMA_FT2201_Point25_BufdataB[1]) & 0xFF);
                        Send_buf[4 + 0x44 + 0x02] = Convert.ToByte(Convert.ToUInt16(GAMMA_FT2201_Point25_BufdataB[2]) & 0xFF);
                        Send_buf[4 + 0x44 + 0x03] = Convert.ToByte(Convert.ToUInt16(GAMMA_FT2201_Point25_BufdataB[3]) & 0xFF);
                        Send_buf[4 + 0x44 + 0x04] = Convert.ToByte(((Convert.ToUInt16(GAMMA_FT2201_Point25_BufdataB[0]) >> 8) << 0)
                            + ((Convert.ToUInt16(GAMMA_FT2201_Point25_BufdataB[1]) >> 8) << 2)
                            + ((Convert.ToUInt16(GAMMA_FT2201_Point25_BufdataB[2]) >> 8) << 4)
                            + ((Convert.ToUInt16(GAMMA_FT2201_Point25_BufdataB[3]) >> 8) << 6));
                        Send_buf[4 + 0x44 + 0x05] = Convert.ToByte(Convert.ToUInt16(GAMMA_FT2201_Point25_BufdataB[4]) & 0xFF);
                        Send_buf[4 + 0x44 + 0x06] = Convert.ToByte(Convert.ToUInt16(GAMMA_FT2201_Point25_BufdataB[5]) & 0xFF);
                        Send_buf[4 + 0x44 + 0x07] = Convert.ToByte(Convert.ToUInt16(GAMMA_FT2201_Point25_BufdataB[6]) & 0xFF);
                        Send_buf[4 + 0x44 + 0x08] = Convert.ToByte(Convert.ToUInt16(GAMMA_FT2201_Point25_BufdataB[7]) & 0xFF);
                        Send_buf[4 + 0x44 + 0x09] = Convert.ToByte(((Convert.ToUInt16(GAMMA_FT2201_Point25_BufdataB[4]) >> 8) << 0)
                            + ((Convert.ToUInt16(GAMMA_FT2201_Point25_BufdataB[5]) >> 8) << 2)
                            + ((Convert.ToUInt16(GAMMA_FT2201_Point25_BufdataB[6]) >> 8) << 4)
                            + ((Convert.ToUInt16(GAMMA_FT2201_Point25_BufdataB[7]) >> 8) << 6));
                        Send_buf[4 + 0x44 + 0x0A] = Convert.ToByte(Convert.ToUInt16(GAMMA_FT2201_Point25_BufdataB[8]) & 0xFF);
                        Send_buf[4 + 0x44 + 0x0B] = Convert.ToByte(Convert.ToUInt16(GAMMA_FT2201_Point25_BufdataB[9]) & 0xFF);
                        Send_buf[4 + 0x44 + 0x0C] = Convert.ToByte(Convert.ToUInt16(GAMMA_FT2201_Point25_BufdataB[10]) & 0xFF);
                        Send_buf[4 + 0x44 + 0x0D] = Convert.ToByte(Convert.ToUInt16(GAMMA_FT2201_Point25_BufdataB[11]) & 0xFF);
                        Send_buf[4 + 0x44 + 0x0E] = Convert.ToByte(((Convert.ToUInt16(GAMMA_FT2201_Point25_BufdataB[8]) >> 8) << 0)
                            + ((Convert.ToUInt16(GAMMA_FT2201_Point25_BufdataB[9]) >> 8) << 2)
                            + ((Convert.ToUInt16(GAMMA_FT2201_Point25_BufdataB[10]) >> 8) << 4)
                            + ((Convert.ToUInt16(GAMMA_FT2201_Point25_BufdataB[11]) >> 8) << 6));
                        Send_buf[4 + 0x44 + 0x0F] = Convert.ToByte(Convert.ToUInt16(GAMMA_FT2201_Point25_BufdataB[12]) & 0xFF);
                        Send_buf[4 + 0x44 + 0x10] = Convert.ToByte(Convert.ToUInt16(GAMMA_FT2201_Point25_BufdataB[13]) & 0xFF);
                        Send_buf[4 + 0x44 + 0x11] = Convert.ToByte(Convert.ToUInt16(GAMMA_FT2201_Point25_BufdataB[14]) & 0xFF);
                        Send_buf[4 + 0x44 + 0x12] = Convert.ToByte(Convert.ToUInt16(GAMMA_FT2201_Point25_BufdataB[15]) & 0xFF);
                        Send_buf[4 + 0x44 + 0x13] = Convert.ToByte(((Convert.ToUInt16(GAMMA_FT2201_Point25_BufdataB[12]) >> 8) << 0)
                            + ((Convert.ToUInt16(GAMMA_FT2201_Point25_BufdataB[13]) >> 8) << 2)
                            + ((Convert.ToUInt16(GAMMA_FT2201_Point25_BufdataB[14]) >> 8) << 4)
                            + ((Convert.ToUInt16(GAMMA_FT2201_Point25_BufdataB[15]) >> 8) << 6));
                        Send_buf[4 + 0x44 + 0x14] = Convert.ToByte(Convert.ToUInt16(GAMMA_FT2201_Point25_BufdataB[16]) & 0xFF);
                        Send_buf[4 + 0x44 + 0x15] = Convert.ToByte(Convert.ToUInt16(GAMMA_FT2201_Point25_BufdataB[17]) & 0xFF);
                        Send_buf[4 + 0x44 + 0x16] = Convert.ToByte(Convert.ToUInt16(GAMMA_FT2201_Point25_BufdataB[18]) & 0xFF);
                        Send_buf[4 + 0x44 + 0x17] = Convert.ToByte(Convert.ToUInt16(GAMMA_FT2201_Point25_BufdataB[19]) & 0xFF);
                        Send_buf[4 + 0x44 + 0x18] = Convert.ToByte(((Convert.ToUInt16(GAMMA_FT2201_Point25_BufdataB[16]) >> 8) << 0)
                            + ((Convert.ToUInt16(GAMMA_FT2201_Point25_BufdataB[17]) >> 8) << 2)
                            + ((Convert.ToUInt16(GAMMA_FT2201_Point25_BufdataB[18]) >> 8) << 4)
                            + ((Convert.ToUInt16(GAMMA_FT2201_Point25_BufdataB[19]) >> 8) << 6));
                        Send_buf[4 + 0x44 + 0x19] = Convert.ToByte(Convert.ToUInt16(GAMMA_FT2201_Point25_BufdataB[20]) & 0xFF);
                        Send_buf[4 + 0x44 + 0x1A] = Convert.ToByte(Convert.ToUInt16(GAMMA_FT2201_Point25_BufdataB[21]) & 0xFF);
                        Send_buf[4 + 0x44 + 0x1B] = Convert.ToByte(Convert.ToUInt16(GAMMA_FT2201_Point25_BufdataB[22]) & 0xFF);
                        Send_buf[4 + 0x44 + 0x1C] = Convert.ToByte(Convert.ToUInt16(GAMMA_FT2201_Point25_BufdataB[23]) & 0xFF);
                        Send_buf[4 + 0x44 + 0x1D] = Convert.ToByte(((Convert.ToUInt16(GAMMA_FT2201_Point25_BufdataB[20]) >> 8) << 0)
                            + ((Convert.ToUInt16(GAMMA_FT2201_Point25_BufdataB[21]) >> 8) << 2)
                            + ((Convert.ToUInt16(GAMMA_FT2201_Point25_BufdataB[22]) >> 8) << 4)
                            + ((Convert.ToUInt16(GAMMA_FT2201_Point25_BufdataB[23]) >> 8) << 6));
                        Send_buf[4 + 0x44 + 0x1E] = Convert.ToByte(Convert.ToUInt16(GAMMA_FT2201_Point25_BufdataB[24]) & 0xFF);
                        Send_buf[4 + 0x44 + 0x1F] = Convert.ToByte(Convert.ToUInt16(GAMMA_FT2201_Point25_BufdataB[25]) & 0xFF);
                        Send_buf[4 + 0x44 + 0x20] = Convert.ToByte(Convert.ToUInt16(GAMMA_FT2201_Point25_BufdataB[26]) & 0xFF);
                        Send_buf[4 + 0x44 + 0x21] = Convert.ToByte(((Convert.ToUInt16(GAMMA_FT2201_Point25_BufdataB[24]) >> 8) << 0)
                            + ((Convert.ToUInt16(GAMMA_FT2201_Point25_BufdataB[25]) >> 8) << 2)
                            + ((Convert.ToUInt16(GAMMA_FT2201_Point25_BufdataB[26]) >> 8) << 4));
                        //Send_buf[4 + 0x69 + 0x31] = Convert.ToByte(Convert.ToUInt16(GAMMA_FT2201_Point25_BufdataB[24]) & 0xFF);


                        if (FT2201_GAMMA_2.Checked == true)
                        {
                            Send_buf[4 + 0x44 + 0x22] = 0xE8;
                        }
                        else
                        {
                            Send_buf[4 + 0x44 + 0x22] = 0xC8;
                        }

                    }
                    break;

                case "RMFT2201_GammaR_all":
                    {
                        //Setting.TempRecord.RMFT2201CmdType = 0x03;       //      3
                        Send_buf[1] = 0x04;                             //读Gamma

                    }
                    break;

                default:
                    break;

            }
            CRCData.CalculateCrc16(Send_buf, out Send_buf[107 + 5 - 2], out Send_buf[107 + 5 - 1]);
            try
            {
                RM350_0XFE_XX = 0x00;
                MainForm.comPort.Write(Send_buf, 0, 107 + 5);   //FT2201
            }
            catch
            {
                MessageBox.Show("串口有误，请检查", "错误");
            }
            T1.Enabled = false;
            T1.Interval = 2000;
            T1.Enabled = true;
        }



        private void trackBar2_Scroll(object sender, EventArgs e)
        {
            Int32 DataNum;

            FT2201_txtRegData.Text = "51," + trackBar_FT2201.Value.ToString("x2") + ",01";
            string strLine = Convert.ToString(FT2201_txtRegData.Text);
            string[] arr = strLine.Split(','); //以逗号分割，添加到数组

            Setting.TempRecord.cmdLen = arr.Length + 5;
            Send_buf = new byte[arr.Length + 5];
            Send_buf[0] = 0x53;
            Send_buf[1] = 0x01;
            Send_buf[2] = byte.Parse(arr.Length.ToString());
            for (int i = 0; i < arr.Length; i++)
            {
                Send_buf[3 + i] = byte.Parse((Convert.ToInt32(arr[i], 16)).ToString());
            }
            CRCData.CalculateCrc16(Send_buf, out Send_buf[5 + arr.Length - 2], out Send_buf[5 + arr.Length - 1]);
            try
            {
                MainForm.comPort.Write(Send_buf, 0, 5 + arr.Length);
            }
            catch
            {
                MessageBox.Show("串口有误，请检查", "错误");
            }


        }

        private static bool Gamma_adjust_timeout_flag = false;
        private static int Gamma_auto_adjust_count = 0;
        //private int flag1 = 1;
        //private int flag2 = 1;
        //private int flag3 = 1;
        private void R6645x_Gamma_auto_Adujst(int lum_adjust_enable, int x_adjust_enable, int y_adjust_enable, int steph, int stepm, int stepl,
        double measure_lum, double measure_x, double measure_y, double target_lum, double target_x, double target_y, int adjust_times,
        decimal input_reg_red, decimal input_reg_green, decimal input_reg_blue,
        //低阶绑点调用
        int gary_last_index2, int gary_last_index1, int gary_target,
        decimal index2_reg_red, decimal index2_reg_green, decimal index2_reg_blue,
        decimal index1_reg_red, decimal index1_reg_green, decimal index1_reg_blue,

        out decimal output_reg_red, out decimal output_reg_green, out decimal output_reg_blue, out bool timeout_flag)
        {
            int step_tmp = 0;

            timeout_flag = false;
            output_reg_red = input_reg_red;
            output_reg_green = input_reg_green;
            output_reg_blue = input_reg_blue;

            try
            {
                if (Gamma_auto_adjust_count <= adjust_times)
                {
                    Gamma_auto_adjust_count = Gamma_auto_adjust_count + 1;
                    if (x_adjust_enable == 1)
                    {
                        {
                            if (Math.Abs(measure_x - target_x) > target_x * 20 / 100)
                            {
                                if (target_lum < 1)
                                { step_tmp = stepm; }
                                else
                                { step_tmp = steph; }
                            }
                            else if (Math.Abs(measure_x - target_x) > target_x * 5 / 100)
                            {
                                if (target_lum < 1) //if lower target lum
                                { step_tmp = stepl; }
                                else
                                { step_tmp = stepm; }
                            }
                            else
                            {
                                step_tmp = stepl;
                            }
                            if (measure_x > target_x)
                                output_reg_red = input_reg_red + step_tmp;
                            else output_reg_red = input_reg_red - step_tmp;
                        }
                    }
                    //-----------------------------------------------
                    if (lum_adjust_enable == 1)
                    {
                        if (Math.Abs(measure_lum - target_lum) > target_lum * 20 / 100)
                        {
                            if (target_lum < 1)
                            { step_tmp = stepm; }
                            else
                            { step_tmp = steph; }
                        }
                        else if (Math.Abs(measure_lum - target_lum) > target_lum * 2 / 100)
                        {
                            if (target_lum < 1)
                            { step_tmp = stepl; }
                            else
                            { step_tmp = stepm; }
                        }
                        else
                        {
                            step_tmp = stepl;
                        }
                        if (measure_lum > target_lum)
                            output_reg_green = input_reg_green + step_tmp;
                        else output_reg_green = input_reg_green - step_tmp;
                    }
                    //-----------------------------------------
                    if (y_adjust_enable == 1)
                    {
                        {
                            if (Math.Abs(measure_y - target_y) > target_y * 20 / 100)
                            {
                                if (target_lum < 1)
                                { step_tmp = stepm; }
                                else
                                { step_tmp = steph; }
                            }
                            else if (Math.Abs(measure_y - target_y) > target_y * 5 / 100)
                            {
                                if (target_lum < 1)
                                { step_tmp = stepl; }
                                else
                                { step_tmp = stepm; }
                            }
                            else
                            {
                                step_tmp = stepl;
                            }
                            if (measure_y > target_y)
                                output_reg_blue = input_reg_blue + step_tmp;
                            else output_reg_blue = input_reg_blue - step_tmp;
                        }
                    }
                    //----------------------------------若数值超限--------------------------------------

                    //if (((output_reg_red < 30) || (output_reg_red > index2_reg_red) || (output_reg_green < 30) || (output_reg_green > index2_reg_green) || (output_reg_blue < 30) || (output_reg_blue > index2_reg_blue)) && ((target_lum * 95 / 100 < measure_lum) && (target_lum * 105 / 100 > measure_lum)))
                    //{
                    //    output_reg_red = Convert.ToInt16(index1_reg_red - Math.Abs(index2_reg_red - index1_reg_red) / (gary_last_index2 - gary_last_index1) * (gary_last_index1 - gary_target));
                    //    output_reg_green = Convert.ToInt16(index1_reg_green - Math.Abs(index2_reg_green - index1_reg_green) / (gary_last_index2 - gary_last_index1) * (gary_last_index1 - gary_target));
                    //    output_reg_blue = Convert.ToInt16(index1_reg_blue - Math.Abs(index2_reg_blue - index1_reg_blue) / (gary_last_index2 - gary_last_index1) * (gary_last_index1 - gary_target));
                    //    textBox1.Text = "\n" + gary_target +"---该绑点R/G/B插值迭代\r";
                    //    Gamma_auto_adjust_count = 0;
                    //    timeout_flag = true;
                    //}
                }
                else
                {
                    textBox1.Text += "\n" + gary_target + " ---Timeout!\r\n";
                    Gamma_auto_adjust_count = 0;
                    timeout_flag = true;

                    if (output_reg_red > index1_reg_red)
                    {
                        output_reg_red = index1_reg_red - Math.Abs(index2_reg_red - index1_reg_red) / (gary_last_index2 - gary_last_index1) * (gary_last_index1 - gary_target);
                        textBox1.Text += "\n" + gary_target + "该绑点R差值迭代\r\n";
                    }
                    if (output_reg_green > index1_reg_green)
                    {
                        output_reg_green = index1_reg_green - Math.Abs(index2_reg_green - index1_reg_green) / (gary_last_index2 - gary_last_index1) * (gary_last_index1 - gary_target);
                        textBox1.Text += "\n" + gary_target + "该绑点G差值迭代\r\n";
                    }
                    if (output_reg_blue > index1_reg_blue)
                    {
                        output_reg_blue = index1_reg_blue - Math.Abs(index2_reg_blue - index1_reg_blue) / (gary_last_index2 - gary_last_index1) * (gary_last_index1 - gary_target);
                        textBox1.Text += "\n" + gary_target + "该绑点B差值迭代\r\n";
                    }
                }
                output_reg_red = Convert.ToInt32(output_reg_red);
                output_reg_green = Convert.ToInt32(output_reg_green);
                output_reg_blue = Convert.ToInt32(output_reg_blue);
            }
            catch
            {
                ;
            }

        }

        void auto_gamma_write(UInt16 addr, UInt16 datar, UInt16 addg, UInt16 datag, UInt16 addb, UInt16 Datab)//一组gamma写入
        {
            Send_buf = new byte[17];
            Setting.TempRecord.cmdLen = 17;

            if ((datar < 0x0001) || (datag < 0x0001) || (Datab < 0x0001) || (datar > 0x03FF) || (datag > 0x03FF) || (Datab > 0x03FF))
            {
                T5.Enabled = false;
                flag1 = 1;
                flag2 = 1;
                flag3 = 1;
                adjust_Rnum = 0;
                adjust_Gnum = 0;
                adjust_Bnum = 0;
                adjust_l = 0;
                adjust_x = 0;
                adjust_y = 0;
                MessageBox.Show("数值超出界限，已停止gamma调试", "提示");
            }

            FT2201_Gamma_Click(FT2201_GammaW_all, null);
            Thread.Sleep(150); //1秒=1000毫秒，


            //switch (MainForm.IC_comboBox.SelectedIndex)
            //{
            //    case 0:
            //        Send_buf[0] = 0x09;//195
            //        break;
            //    case 1:
            //        Send_buf[0] = 0x08;//120
            //        break;
            //    case 2:
            //        Send_buf[0] = 0x0E;//160
            //        break;
            //    case 3:
            //        Send_buf[0] = 0x1E;//162
            //        break;
            //    case 4:
            //        Send_buf[0] = 0x19;//295
            //        break;
            //    case 5:
            //        Send_buf[0] = 0x29;//198
            //        break;
            //    case 8:
            //        Send_buf[0] = 0x2C;//300
            //        break;
            //    case 10:
            //        Send_buf[0] = 0x2e;//298
            //        break;
            //    case 11:
            //        Send_buf[0] = 0x3C;//350
            //        break;
            //    case 12:
            //        Send_buf[0] = 0x3D;//010
            //        break;
            //    case 13:
            //        Send_buf[0] = 0x30;//R66455
            //        break;
            //    case 18:
            //        Send_buf[0] = 0x52;//RM69310
            //        break;
            //    default:
            //        break;
            //}
            //Send_buf[1] = 0xDD;
            //Send_buf[2] = 12;
            //Send_buf[3] = Convert.ToByte(addr >> 8);
            //Send_buf[4] = Convert.ToByte(datar >> 8);
            //Send_buf[5] = Convert.ToByte(addr & 0xff);
            //Send_buf[6] = Convert.ToByte(datar & 0xff);
            //Send_buf[7] = Convert.ToByte(addg >> 8);
            //Send_buf[8] = Convert.ToByte(datag >> 8);
            //Send_buf[9] = Convert.ToByte(addg & 0xff);
            //Send_buf[10] = Convert.ToByte(datag & 0xff);
            //Send_buf[11] = Convert.ToByte(addb >> 8);
            //Send_buf[12] = Convert.ToByte(Datab >> 8);
            //Send_buf[13] = Convert.ToByte(addb & 0xff);
            //Send_buf[14] = Convert.ToByte(Datab & 0xff);
            //CRCData.CalculateCrc16(Send_buf, out Send_buf[15], out Send_buf[16]);

            //try
            //{
            //    MainForm.comPort.Write(Send_buf, 0, 17);
            //}
            //catch
            //{
            //    MessageBox.Show("串口有误，请检查", "错误");
            //}
            System.Threading.Thread.Sleep(25); //1秒=1000毫秒，
        }


        private void RM350_OTP_Click(object sender, EventArgs e)
        {

        }

        private void btn310Stop_Click(object sender, EventArgs e)
        {

            T5.Enabled = false;
            flag1 = 1;
            flag2 = 1;
            flag3 = 1;
            adjust_Rnum = 0;
            adjust_Gnum = 0;
            adjust_Bnum = 0;
            adjust_l = 0;
            adjust_x = 0;
            adjust_y = 0;
            //--------reset---------------
            Gamma_adjust_timeout_flag = false;
            Gamma_auto_adjust_count = 0;
            //----------------------------

        }



        private void radioButton12_CheckedChanged(object sender, EventArgs e)
        {
            RadioButton btnTmp = (RadioButton)sender;


        }

        private void mtLoadGamma_Click(object sender, EventArgs e)
        {
            OpenFileDialog of = new OpenFileDialog();
            string strtmp = "";
            of.Filter = @"csv文件(*.csv)|*.csv|所有文件(*.*)|*.*"; ;
            of.Title = "打开Gamma文件";

            if (of.ShowDialog() == DialogResult.OK)
            {
                //读取文本信息
                FileStream fs = new FileStream(of.FileName, FileMode.Open, FileAccess.Read);
                StreamReader streamReader = new StreamReader(fs, Encoding.GetEncoding("GBK"));
                streamReader.BaseStream.Seek(0, SeekOrigin.Begin);
                string strLine = streamReader.ReadLine();
                //循环读取文本信息
                while (!string.IsNullOrEmpty(strLine))
                {
                    strtmp = strtmp + strLine + ",";
                    strLine = streamReader.ReadLine(); //循环读取下一行
                }
                fs.Close(); //关闭流
                streamReader.Close();

                string[] arrtmp = strtmp.Split(new Char[] { ',' });

                if (MainForm.IC_comboBox.SelectedIndex == Setting.IC_Type.FT2201)
                {
                    FT2201_gammaR0.Text = arrtmp[1].ToString(); FT2201_gammaG0.Text = arrtmp[3].ToString(); FT2201_gammaB0.Text = arrtmp[5].ToString();
                    FT2201_gammaR1.Text = arrtmp[7].ToString(); FT2201_gammaG1.Text = arrtmp[9].ToString(); FT2201_gammaB1.Text = arrtmp[11].ToString();
                    FT2201_gammaR2.Text = arrtmp[13].ToString(); FT2201_gammaG2.Text = arrtmp[15].ToString(); FT2201_gammaB2.Text = arrtmp[17].ToString();
                    FT2201_gammaR3.Text = arrtmp[19].ToString(); FT2201_gammaG3.Text = arrtmp[21].ToString(); FT2201_gammaB3.Text = arrtmp[23].ToString();
                    FT2201_gammaR4.Text = arrtmp[25].ToString(); FT2201_gammaG4.Text = arrtmp[27].ToString(); FT2201_gammaB4.Text = arrtmp[29].ToString();
                    FT2201_gammaR5.Text = arrtmp[31].ToString(); FT2201_gammaG5.Text = arrtmp[33].ToString(); FT2201_gammaB5.Text = arrtmp[35].ToString();
                    FT2201_gammaR6.Text = arrtmp[37].ToString(); FT2201_gammaG6.Text = arrtmp[39].ToString(); FT2201_gammaB6.Text = arrtmp[41].ToString();
                    FT2201_gammaR7.Text = arrtmp[43].ToString(); FT2201_gammaG7.Text = arrtmp[45].ToString(); FT2201_gammaB7.Text = arrtmp[47].ToString();
                    FT2201_gammaR8.Text = arrtmp[49].ToString(); FT2201_gammaG8.Text = arrtmp[51].ToString(); FT2201_gammaB8.Text = arrtmp[53].ToString();
                    FT2201_gammaR9.Text = arrtmp[55].ToString(); FT2201_gammaG9.Text = arrtmp[57].ToString(); FT2201_gammaB9.Text = arrtmp[59].ToString();
                    FT2201_gammaR10.Text = arrtmp[61].ToString(); FT2201_gammaG10.Text = arrtmp[63].ToString(); FT2201_gammaB10.Text = arrtmp[65].ToString();
                    FT2201_gammaR11.Text = arrtmp[67].ToString(); FT2201_gammaG11.Text = arrtmp[69].ToString(); FT2201_gammaB11.Text = arrtmp[71].ToString();
                    FT2201_gammaR12.Text = arrtmp[73].ToString(); FT2201_gammaG12.Text = arrtmp[75].ToString(); FT2201_gammaB12.Text = arrtmp[77].ToString();
                    FT2201_gammaR13.Text = arrtmp[79].ToString(); FT2201_gammaG13.Text = arrtmp[81].ToString(); FT2201_gammaB13.Text = arrtmp[83].ToString();
                    FT2201_gammaR14.Text = arrtmp[85].ToString(); FT2201_gammaG14.Text = arrtmp[87].ToString(); FT2201_gammaB14.Text = arrtmp[89].ToString();
                    FT2201_gammaR15.Text = arrtmp[91].ToString(); FT2201_gammaG15.Text = arrtmp[93].ToString(); FT2201_gammaB15.Text = arrtmp[95].ToString();
                    FT2201_gammaR16.Text = arrtmp[97].ToString(); FT2201_gammaG16.Text = arrtmp[99].ToString(); FT2201_gammaB16.Text = arrtmp[101].ToString();
                    FT2201_gammaR17.Text = arrtmp[103].ToString(); FT2201_gammaG17.Text = arrtmp[105].ToString(); FT2201_gammaB17.Text = arrtmp[107].ToString();
                    FT2201_gammaR18.Text = arrtmp[109].ToString(); FT2201_gammaG18.Text = arrtmp[111].ToString(); FT2201_gammaB18.Text = arrtmp[113].ToString();
                    FT2201_gammaR19.Text = arrtmp[115].ToString(); FT2201_gammaG19.Text = arrtmp[117].ToString(); FT2201_gammaB19.Text = arrtmp[119].ToString();
                    FT2201_gammaR20.Text = arrtmp[121].ToString(); FT2201_gammaG20.Text = arrtmp[123].ToString(); FT2201_gammaB20.Text = arrtmp[125].ToString();
                    FT2201_gammaR21.Text = arrtmp[127].ToString(); FT2201_gammaG21.Text = arrtmp[129].ToString(); FT2201_gammaB21.Text = arrtmp[131].ToString();
                    FT2201_gammaR22.Text = arrtmp[133].ToString(); FT2201_gammaG22.Text = arrtmp[135].ToString(); FT2201_gammaB22.Text = arrtmp[137].ToString();
                    FT2201_gammaR23.Text = arrtmp[139].ToString(); FT2201_gammaG23.Text = arrtmp[141].ToString(); FT2201_gammaB23.Text = arrtmp[143].ToString();
                    FT2201_gammaR24.Text = arrtmp[145].ToString(); FT2201_gammaG24.Text = arrtmp[147].ToString(); FT2201_gammaB24.Text = arrtmp[149].ToString();
                    FT2201_gammaR25.Text = arrtmp[151].ToString(); FT2201_gammaG25.Text = arrtmp[153].ToString(); FT2201_gammaB25.Text = arrtmp[155].ToString();
                    FT2201_gammaR26.Text = arrtmp[157].ToString(); FT2201_gammaG26.Text = arrtmp[159].ToString(); FT2201_gammaB26.Text = arrtmp[161].ToString();

                    {
                        //FT2201_txtRegData.Text = arrtmp[172].ToString() + ",";
                        ////tmp = String.Format("{0:X2}", Rec_tmp[4]);//16进制
                        //FT2201_gammaR0.Text = arrtmp[174 + 2 * 0].ToString()+ ((Convert.ToUInt16(arrtmp[174+2*4].ToString())&0xc0)<<2);
                        //FT2201_gammaR1.Text = arrtmp[174 + 2 * 1].ToString() + ((Convert.ToUInt16(arrtmp[174 + 2 * 4].ToString()) & 0x30) << 4);
                        //FT2201_gammaR2.Text = arrtmp[174 + 2 * 2].ToString() + ((Convert.ToUInt16(arrtmp[174 + 2 * 4].ToString()) & 0x0c) << 6);
                        //FT2201_gammaR3.Text = arrtmp[174 + 2 * 3].ToString() + ((Convert.ToUInt16(arrtmp[174 + 2 * 4].ToString()) & 0x03) << 8);

                        //for (int i = 0; i < 0x61; i++)
                        //{
                        //    FT2201_txtRegData.Text = FT2201_txtRegData.Text + arrtmp[174 + i * 2 + 0].ToString() + ",";
                        //}
                    }
                }
                {

                }
            }
        }

        private void mtSaveGamma_Click(object sender, EventArgs e)
        {
            SaveFileDialog sf = new SaveFileDialog(); //对话框
            sf.Filter = @"csv文件(*.csv)|*.csv|所有文件(*.*)|*.*"; //定义保存的文件的类型
            Send_buf = new byte[108];
            int max_grey_level = 0x3ff;
            string str = "";
            if (sf.ShowDialog() == DialogResult.OK) //如果确定保存
            {
                if (sf.FileName == "") return; //如果没有输入文件名
                try
                {
                    StreamWriter sw = null; //文件流
                    sw = new StreamWriter(sf.FileName, false, Encoding.UTF8);
                    if (MainForm.IC_comboBox.SelectedIndex == Setting.IC_Type.FT2201) //ft2201
                    {
                        str = str + "R000," + FT2201_gammaR0.Text + "," + "G000," + FT2201_gammaG0.Text + "," + "B000," + FT2201_gammaB0.Text + "\r\n";
                        str = str + "R004," + FT2201_gammaR1.Text + "," + "G004," + FT2201_gammaG1.Text + "," + "B004," + FT2201_gammaB1.Text + "\r\n";
                        str = str + "R008," + FT2201_gammaR2.Text + "," + "G008," + FT2201_gammaG2.Text + "," + "B008," + FT2201_gammaB2.Text + "\r\n";
                        str = str + "R012," + FT2201_gammaR3.Text + "," + "G012," + FT2201_gammaG3.Text + "," + "B012," + FT2201_gammaB3.Text + "\r\n";
                        str = str + "R016," + FT2201_gammaR4.Text + "," + "G016," + FT2201_gammaG4.Text + "," + "B016," + FT2201_gammaB4.Text + "\r\n";
                        str = str + "R024," + FT2201_gammaR5.Text + "," + "G024," + FT2201_gammaG5.Text + "," + "B024," + FT2201_gammaB5.Text + "\r\n";
                        str = str + "R032," + FT2201_gammaR6.Text + "," + "G032," + FT2201_gammaG6.Text + "," + "B032," + FT2201_gammaB6.Text + "\r\n";
                        str = str + "R040," + FT2201_gammaR7.Text + "," + "G040," + FT2201_gammaG7.Text + "," + "B040," + FT2201_gammaB7.Text + "\r\n";
                        str = str + "R048," + FT2201_gammaR8.Text + "," + "G048," + FT2201_gammaG8.Text + "," + "B048," + FT2201_gammaB8.Text + "\r\n";
                        str = str + "R064," + FT2201_gammaR9.Text + "," + "G064," + FT2201_gammaG9.Text + "," + "B064," + FT2201_gammaB9.Text + "\r\n";
                        str = str + "R080," + FT2201_gammaR10.Text + "," + "G080," + FT2201_gammaG10.Text + "," + "B080," + FT2201_gammaB10.Text + "\r\n";
                        str = str + "R096," + FT2201_gammaR11.Text + "," + "G096," + FT2201_gammaG11.Text + "," + "B096," + FT2201_gammaB11.Text + "\r\n";
                        str = str + "R112," + FT2201_gammaR12.Text + "," + "G112," + FT2201_gammaG12.Text + "," + "B112," + FT2201_gammaB12.Text + "\r\n";
                        str = str + "R128," + FT2201_gammaR13.Text + "," + "G128," + FT2201_gammaG13.Text + "," + "B128," + FT2201_gammaB13.Text + "\r\n";
                        str = str + "R144," + FT2201_gammaR14.Text + "," + "G144," + FT2201_gammaG14.Text + "," + "B144," + FT2201_gammaB14.Text + "\r\n";
                        str = str + "R160," + FT2201_gammaR15.Text + "," + "G160," + FT2201_gammaG15.Text + "," + "B160," + FT2201_gammaB15.Text + "\r\n";
                        str = str + "R176," + FT2201_gammaR16.Text + "," + "G176," + FT2201_gammaG16.Text + "," + "B176," + FT2201_gammaB16.Text + "\r\n";
                        str = str + "R192," + FT2201_gammaR17.Text + "," + "G192," + FT2201_gammaG17.Text + "," + "B192," + FT2201_gammaB17.Text + "\r\n";
                        str = str + "R208," + FT2201_gammaR18.Text + "," + "G208," + FT2201_gammaG18.Text + "," + "B208," + FT2201_gammaB18.Text + "\r\n";
                        str = str + "R218," + FT2201_gammaR19.Text + "," + "G218," + FT2201_gammaG19.Text + "," + "B218," + FT2201_gammaB19.Text + "\r\n";
                        str = str + "R224," + FT2201_gammaR20.Text + "," + "G224," + FT2201_gammaG20.Text + "," + "B224," + FT2201_gammaB20.Text + "\r\n";
                        str = str + "R232," + FT2201_gammaR21.Text + "," + "G232," + FT2201_gammaG22.Text + "," + "B232," + FT2201_gammaB21.Text + "\r\n";
                        str = str + "R240," + FT2201_gammaR22.Text + "," + "G240," + FT2201_gammaG22.Text + "," + "B240," + FT2201_gammaB22.Text + "\r\n";
                        str = str + "R248," + FT2201_gammaR23.Text + "," + "G248," + FT2201_gammaG23.Text + "," + "B248," + FT2201_gammaB23.Text + "\r\n";
                        str = str + "R252," + FT2201_gammaR24.Text + "," + "G252," + FT2201_gammaG24.Text + "," + "B252," + FT2201_gammaB24.Text + "\r\n";
                        str = str + "R254," + FT2201_gammaR25.Text + "," + "G254," + FT2201_gammaG25.Text + "," + "B254," + FT2201_gammaB25.Text + "\r\n";
                        str = str + "R255," + FT2201_gammaR26.Text + "," + "G255," + FT2201_gammaG26.Text + "," + "B255," + FT2201_gammaB26.Text + "\r\n";

                        str = str + "," + "\r\n";
                        str = str + "," + "\r\n";
                        str = str + "," + "\r\n";
                        str = str + "," + "\r\n";
                        str = str + "," + "\r\n";

                        ////R  gamma
                        GAMMA_FT2201_Point25_BufdataR[0] = max_grey_level & (Convert.ToUInt16(FT2201_gammaR0.Value.ToString()));
                        GAMMA_FT2201_Point25_BufdataR[1] = max_grey_level & (Convert.ToUInt16(FT2201_gammaR1.Value.ToString()));
                        GAMMA_FT2201_Point25_BufdataR[2] = max_grey_level & (Convert.ToUInt16(FT2201_gammaR2.Value.ToString()));
                        GAMMA_FT2201_Point25_BufdataR[3] = max_grey_level & (Convert.ToUInt16(FT2201_gammaR3.Value.ToString()));
                        GAMMA_FT2201_Point25_BufdataR[4] = max_grey_level & (Convert.ToUInt16(FT2201_gammaR4.Value.ToString()));
                        GAMMA_FT2201_Point25_BufdataR[5] = max_grey_level & (Convert.ToUInt16(FT2201_gammaR5.Value.ToString()));
                        GAMMA_FT2201_Point25_BufdataR[6] = max_grey_level & (Convert.ToUInt16(FT2201_gammaR6.Value.ToString()));
                        GAMMA_FT2201_Point25_BufdataR[7] = max_grey_level & (Convert.ToUInt16(FT2201_gammaR7.Value.ToString()));
                        GAMMA_FT2201_Point25_BufdataR[8] = max_grey_level & (Convert.ToUInt16(FT2201_gammaR8.Value.ToString()));
                        GAMMA_FT2201_Point25_BufdataR[9] = max_grey_level & (Convert.ToUInt16(FT2201_gammaR9.Value.ToString()));
                        GAMMA_FT2201_Point25_BufdataR[10] = max_grey_level & (Convert.ToUInt16(FT2201_gammaR10.Value.ToString()));
                        GAMMA_FT2201_Point25_BufdataR[11] = max_grey_level & (Convert.ToUInt16(FT2201_gammaR11.Value.ToString()));
                        GAMMA_FT2201_Point25_BufdataR[12] = max_grey_level & (Convert.ToUInt16(FT2201_gammaR12.Value.ToString()));
                        GAMMA_FT2201_Point25_BufdataR[13] = max_grey_level & (Convert.ToUInt16(FT2201_gammaR13.Value.ToString()));
                        GAMMA_FT2201_Point25_BufdataR[14] = max_grey_level & (Convert.ToUInt16(FT2201_gammaR14.Value.ToString()));
                        GAMMA_FT2201_Point25_BufdataR[15] = max_grey_level & (Convert.ToUInt16(FT2201_gammaR15.Value.ToString()));
                        GAMMA_FT2201_Point25_BufdataR[16] = max_grey_level & (Convert.ToUInt16(FT2201_gammaR16.Value.ToString()));
                        GAMMA_FT2201_Point25_BufdataR[17] = max_grey_level & (Convert.ToUInt16(FT2201_gammaR17.Value.ToString()));
                        GAMMA_FT2201_Point25_BufdataR[18] = max_grey_level & (Convert.ToUInt16(FT2201_gammaR18.Value.ToString()));
                        GAMMA_FT2201_Point25_BufdataR[19] = max_grey_level & (Convert.ToUInt16(FT2201_gammaR19.Value.ToString()));
                        GAMMA_FT2201_Point25_BufdataR[20] = max_grey_level & (Convert.ToUInt16(FT2201_gammaR20.Value.ToString()));
                        GAMMA_FT2201_Point25_BufdataR[21] = max_grey_level & (Convert.ToUInt16(FT2201_gammaR21.Value.ToString()));
                        GAMMA_FT2201_Point25_BufdataR[22] = max_grey_level & (Convert.ToUInt16(FT2201_gammaR22.Value.ToString()));
                        GAMMA_FT2201_Point25_BufdataR[23] = max_grey_level & (Convert.ToUInt16(FT2201_gammaR23.Value.ToString()));
                        GAMMA_FT2201_Point25_BufdataR[24] = max_grey_level & (Convert.ToUInt16(FT2201_gammaR24.Value.ToString()));
                        GAMMA_FT2201_Point25_BufdataR[25] = max_grey_level & (Convert.ToUInt16(FT2201_gammaR25.Value.ToString()));
                        GAMMA_FT2201_Point25_BufdataR[26] = max_grey_level & (Convert.ToUInt16(FT2201_gammaR26.Value.ToString()));

                        ////G  gamma
                        GAMMA_FT2201_Point25_BufdataG[0] = max_grey_level & (Convert.ToUInt16(FT2201_gammaG0.Value.ToString()));
                        GAMMA_FT2201_Point25_BufdataG[1] = max_grey_level & (Convert.ToUInt16(FT2201_gammaG1.Value.ToString()));
                        GAMMA_FT2201_Point25_BufdataG[2] = max_grey_level & (Convert.ToUInt16(FT2201_gammaG2.Value.ToString()));
                        GAMMA_FT2201_Point25_BufdataG[3] = max_grey_level & (Convert.ToUInt16(FT2201_gammaG3.Value.ToString()));
                        GAMMA_FT2201_Point25_BufdataG[4] = max_grey_level & (Convert.ToUInt16(FT2201_gammaG4.Value.ToString()));
                        GAMMA_FT2201_Point25_BufdataG[5] = max_grey_level & (Convert.ToUInt16(FT2201_gammaG5.Value.ToString()));
                        GAMMA_FT2201_Point25_BufdataG[6] = max_grey_level & (Convert.ToUInt16(FT2201_gammaG6.Value.ToString()));
                        GAMMA_FT2201_Point25_BufdataG[7] = max_grey_level & (Convert.ToUInt16(FT2201_gammaG7.Value.ToString()));
                        GAMMA_FT2201_Point25_BufdataG[8] = max_grey_level & (Convert.ToUInt16(FT2201_gammaG8.Value.ToString()));
                        GAMMA_FT2201_Point25_BufdataG[9] = max_grey_level & (Convert.ToUInt16(FT2201_gammaG9.Value.ToString()));
                        GAMMA_FT2201_Point25_BufdataG[10] = max_grey_level & (Convert.ToUInt16(FT2201_gammaG10.Value.ToString()));
                        GAMMA_FT2201_Point25_BufdataG[11] = max_grey_level & (Convert.ToUInt16(FT2201_gammaG11.Value.ToString()));
                        GAMMA_FT2201_Point25_BufdataG[12] = max_grey_level & (Convert.ToUInt16(FT2201_gammaG12.Value.ToString()));
                        GAMMA_FT2201_Point25_BufdataG[13] = max_grey_level & (Convert.ToUInt16(FT2201_gammaG13.Value.ToString()));
                        GAMMA_FT2201_Point25_BufdataG[14] = max_grey_level & (Convert.ToUInt16(FT2201_gammaG14.Value.ToString()));
                        GAMMA_FT2201_Point25_BufdataG[15] = max_grey_level & (Convert.ToUInt16(FT2201_gammaG15.Value.ToString()));
                        GAMMA_FT2201_Point25_BufdataG[16] = max_grey_level & (Convert.ToUInt16(FT2201_gammaG16.Value.ToString()));
                        GAMMA_FT2201_Point25_BufdataG[17] = max_grey_level & (Convert.ToUInt16(FT2201_gammaG17.Value.ToString()));
                        GAMMA_FT2201_Point25_BufdataG[18] = max_grey_level & (Convert.ToUInt16(FT2201_gammaG18.Value.ToString()));
                        GAMMA_FT2201_Point25_BufdataG[19] = max_grey_level & (Convert.ToUInt16(FT2201_gammaG19.Value.ToString()));
                        GAMMA_FT2201_Point25_BufdataG[20] = max_grey_level & (Convert.ToUInt16(FT2201_gammaG20.Value.ToString()));
                        GAMMA_FT2201_Point25_BufdataG[21] = max_grey_level & (Convert.ToUInt16(FT2201_gammaG21.Value.ToString()));
                        GAMMA_FT2201_Point25_BufdataG[22] = max_grey_level & (Convert.ToUInt16(FT2201_gammaG22.Value.ToString()));
                        GAMMA_FT2201_Point25_BufdataG[23] = max_grey_level & (Convert.ToUInt16(FT2201_gammaG23.Value.ToString()));
                        GAMMA_FT2201_Point25_BufdataG[24] = max_grey_level & (Convert.ToUInt16(FT2201_gammaG24.Value.ToString()));
                        GAMMA_FT2201_Point25_BufdataG[25] = max_grey_level & (Convert.ToUInt16(FT2201_gammaG25.Value.ToString()));
                        GAMMA_FT2201_Point25_BufdataG[26] = max_grey_level & (Convert.ToUInt16(FT2201_gammaG26.Value.ToString()));

                        ////B  gamma
                        GAMMA_FT2201_Point25_BufdataB[0] = max_grey_level & (Convert.ToUInt16(FT2201_gammaB0.Value.ToString()));
                        GAMMA_FT2201_Point25_BufdataB[1] = max_grey_level & (Convert.ToUInt16(FT2201_gammaB1.Value.ToString()));
                        GAMMA_FT2201_Point25_BufdataB[2] = max_grey_level & (Convert.ToUInt16(FT2201_gammaB2.Value.ToString()));
                        GAMMA_FT2201_Point25_BufdataB[3] = max_grey_level & (Convert.ToUInt16(FT2201_gammaB3.Value.ToString()));
                        GAMMA_FT2201_Point25_BufdataB[4] = max_grey_level & (Convert.ToUInt16(FT2201_gammaB4.Value.ToString()));
                        GAMMA_FT2201_Point25_BufdataB[5] = max_grey_level & (Convert.ToUInt16(FT2201_gammaB5.Value.ToString()));
                        GAMMA_FT2201_Point25_BufdataB[6] = max_grey_level & (Convert.ToUInt16(FT2201_gammaB6.Value.ToString()));
                        GAMMA_FT2201_Point25_BufdataB[7] = max_grey_level & (Convert.ToUInt16(FT2201_gammaB7.Value.ToString()));
                        GAMMA_FT2201_Point25_BufdataB[8] = max_grey_level & (Convert.ToUInt16(FT2201_gammaB8.Value.ToString()));
                        GAMMA_FT2201_Point25_BufdataB[9] = max_grey_level & (Convert.ToUInt16(FT2201_gammaB9.Value.ToString()));
                        GAMMA_FT2201_Point25_BufdataB[10] = max_grey_level & (Convert.ToUInt16(FT2201_gammaB10.Value.ToString()));
                        GAMMA_FT2201_Point25_BufdataB[11] = max_grey_level & (Convert.ToUInt16(FT2201_gammaB11.Value.ToString()));
                        GAMMA_FT2201_Point25_BufdataB[12] = max_grey_level & (Convert.ToUInt16(FT2201_gammaB12.Value.ToString()));
                        GAMMA_FT2201_Point25_BufdataB[13] = max_grey_level & (Convert.ToUInt16(FT2201_gammaB13.Value.ToString()));
                        GAMMA_FT2201_Point25_BufdataB[14] = max_grey_level & (Convert.ToUInt16(FT2201_gammaB14.Value.ToString()));
                        GAMMA_FT2201_Point25_BufdataB[15] = max_grey_level & (Convert.ToUInt16(FT2201_gammaB15.Value.ToString()));
                        GAMMA_FT2201_Point25_BufdataB[16] = max_grey_level & (Convert.ToUInt16(FT2201_gammaB16.Value.ToString()));
                        GAMMA_FT2201_Point25_BufdataB[17] = max_grey_level & (Convert.ToUInt16(FT2201_gammaB17.Value.ToString()));
                        GAMMA_FT2201_Point25_BufdataB[18] = max_grey_level & (Convert.ToUInt16(FT2201_gammaB18.Value.ToString()));
                        GAMMA_FT2201_Point25_BufdataB[19] = max_grey_level & (Convert.ToUInt16(FT2201_gammaB19.Value.ToString()));
                        GAMMA_FT2201_Point25_BufdataB[20] = max_grey_level & (Convert.ToUInt16(FT2201_gammaB20.Value.ToString()));
                        GAMMA_FT2201_Point25_BufdataB[21] = max_grey_level & (Convert.ToUInt16(FT2201_gammaB21.Value.ToString()));
                        GAMMA_FT2201_Point25_BufdataB[22] = max_grey_level & (Convert.ToUInt16(FT2201_gammaB22.Value.ToString()));
                        GAMMA_FT2201_Point25_BufdataB[23] = max_grey_level & (Convert.ToUInt16(FT2201_gammaB23.Value.ToString()));
                        GAMMA_FT2201_Point25_BufdataB[24] = max_grey_level & (Convert.ToUInt16(FT2201_gammaB24.Value.ToString()));
                        GAMMA_FT2201_Point25_BufdataB[25] = max_grey_level & (Convert.ToUInt16(FT2201_gammaB25.Value.ToString()));
                        GAMMA_FT2201_Point25_BufdataB[26] = max_grey_level & (Convert.ToUInt16(FT2201_gammaB26.Value.ToString()));

                        //////R
                        Send_buf[4 + 0x00] = Convert.ToByte(Convert.ToUInt16(GAMMA_FT2201_Point25_BufdataR[0]) & 0xFF);
                        Send_buf[4 + 0x01] = Convert.ToByte(Convert.ToUInt16(GAMMA_FT2201_Point25_BufdataR[1]) & 0xFF);
                        Send_buf[4 + 0x02] = Convert.ToByte(Convert.ToUInt16(GAMMA_FT2201_Point25_BufdataR[2]) & 0xFF);
                        Send_buf[4 + 0x03] = Convert.ToByte(Convert.ToUInt16(GAMMA_FT2201_Point25_BufdataR[3]) & 0xFF);
                        Send_buf[4 + 0x04] = Convert.ToByte(((Convert.ToUInt16(GAMMA_FT2201_Point25_BufdataR[0]) >> 8) << 0)
                            + ((Convert.ToUInt16(GAMMA_FT2201_Point25_BufdataR[1]) >> 8) << 2)
                            + ((Convert.ToUInt16(GAMMA_FT2201_Point25_BufdataR[2]) >> 8) << 4)
                            + ((Convert.ToUInt16(GAMMA_FT2201_Point25_BufdataR[3]) >> 8) << 6));
                        Send_buf[4 + 0x05] = Convert.ToByte(Convert.ToUInt16(GAMMA_FT2201_Point25_BufdataR[4]) & 0xFF);
                        Send_buf[4 + 0x06] = Convert.ToByte(Convert.ToUInt16(GAMMA_FT2201_Point25_BufdataR[5]) & 0xFF);
                        Send_buf[4 + 0x07] = Convert.ToByte(Convert.ToUInt16(GAMMA_FT2201_Point25_BufdataR[6]) & 0xFF);
                        Send_buf[4 + 0x08] = Convert.ToByte(Convert.ToUInt16(GAMMA_FT2201_Point25_BufdataR[7]) & 0xFF);
                        Send_buf[4 + 0x09] = Convert.ToByte(((Convert.ToUInt16(GAMMA_FT2201_Point25_BufdataR[4]) >> 8) << 0)
                            + ((Convert.ToUInt16(GAMMA_FT2201_Point25_BufdataR[5]) >> 8) << 2)
                            + ((Convert.ToUInt16(GAMMA_FT2201_Point25_BufdataR[6]) >> 8) << 4)
                            + ((Convert.ToUInt16(GAMMA_FT2201_Point25_BufdataR[7]) >> 8) << 6));
                        Send_buf[4 + 0x0A] = Convert.ToByte(Convert.ToUInt16(GAMMA_FT2201_Point25_BufdataR[8]) & 0xFF);
                        Send_buf[4 + 0x0B] = Convert.ToByte(Convert.ToUInt16(GAMMA_FT2201_Point25_BufdataR[9]) & 0xFF);
                        Send_buf[4 + 0x0C] = Convert.ToByte(Convert.ToUInt16(GAMMA_FT2201_Point25_BufdataR[10]) & 0xFF);
                        Send_buf[4 + 0x0D] = Convert.ToByte(Convert.ToUInt16(GAMMA_FT2201_Point25_BufdataR[11]) & 0xFF);
                        Send_buf[4 + 0x0E] = Convert.ToByte(((Convert.ToUInt16(GAMMA_FT2201_Point25_BufdataR[8]) >> 8) << 0)
                            + ((Convert.ToUInt16(GAMMA_FT2201_Point25_BufdataR[9]) >> 8) << 2)
                            + ((Convert.ToUInt16(GAMMA_FT2201_Point25_BufdataR[10]) >> 8) << 4)
                            + ((Convert.ToUInt16(GAMMA_FT2201_Point25_BufdataR[11]) >> 8) << 6));
                        Send_buf[4 + 0x0F] = Convert.ToByte(Convert.ToUInt16(GAMMA_FT2201_Point25_BufdataR[12]) & 0xFF);
                        Send_buf[4 + 0x10] = Convert.ToByte(Convert.ToUInt16(GAMMA_FT2201_Point25_BufdataR[13]) & 0xFF);
                        Send_buf[4 + 0x11] = Convert.ToByte(Convert.ToUInt16(GAMMA_FT2201_Point25_BufdataR[14]) & 0xFF);
                        Send_buf[4 + 0x12] = Convert.ToByte(Convert.ToUInt16(GAMMA_FT2201_Point25_BufdataR[15]) & 0xFF);
                        Send_buf[4 + 0x13] = Convert.ToByte(((Convert.ToUInt16(GAMMA_FT2201_Point25_BufdataR[12]) >> 8) << 0)
                            + ((Convert.ToUInt16(GAMMA_FT2201_Point25_BufdataR[13]) >> 8) << 2)
                            + ((Convert.ToUInt16(GAMMA_FT2201_Point25_BufdataR[14]) >> 8) << 4)
                            + ((Convert.ToUInt16(GAMMA_FT2201_Point25_BufdataR[15]) >> 8) << 6));
                        Send_buf[4 + 0x14] = Convert.ToByte(Convert.ToUInt16(GAMMA_FT2201_Point25_BufdataR[16]) & 0xFF);
                        Send_buf[4 + 0x15] = Convert.ToByte(Convert.ToUInt16(GAMMA_FT2201_Point25_BufdataR[17]) & 0xFF);
                        Send_buf[4 + 0x16] = Convert.ToByte(Convert.ToUInt16(GAMMA_FT2201_Point25_BufdataR[18]) & 0xFF);
                        Send_buf[4 + 0x17] = Convert.ToByte(Convert.ToUInt16(GAMMA_FT2201_Point25_BufdataR[19]) & 0xFF);
                        Send_buf[4 + 0x18] = Convert.ToByte(((Convert.ToUInt16(GAMMA_FT2201_Point25_BufdataR[16]) >> 8) << 0)
                            + ((Convert.ToUInt16(GAMMA_FT2201_Point25_BufdataR[17]) >> 8) << 2)
                            + ((Convert.ToUInt16(GAMMA_FT2201_Point25_BufdataR[18]) >> 8) << 4)
                            + ((Convert.ToUInt16(GAMMA_FT2201_Point25_BufdataR[19]) >> 8) << 6));
                        Send_buf[4 + 0x19] = Convert.ToByte(Convert.ToUInt16(GAMMA_FT2201_Point25_BufdataR[20]) & 0xFF);
                        Send_buf[4 + 0x1A] = Convert.ToByte(Convert.ToUInt16(GAMMA_FT2201_Point25_BufdataR[21]) & 0xFF);
                        Send_buf[4 + 0x1B] = Convert.ToByte(Convert.ToUInt16(GAMMA_FT2201_Point25_BufdataR[22]) & 0xFF);
                        Send_buf[4 + 0x1C] = Convert.ToByte(Convert.ToUInt16(GAMMA_FT2201_Point25_BufdataR[23]) & 0xFF);
                        Send_buf[4 + 0x1D] = Convert.ToByte(((Convert.ToUInt16(GAMMA_FT2201_Point25_BufdataR[20]) >> 8) << 0)
                            + ((Convert.ToUInt16(GAMMA_FT2201_Point25_BufdataR[21]) >> 8) << 2)
                            + ((Convert.ToUInt16(GAMMA_FT2201_Point25_BufdataR[22]) >> 8) << 4)
                            + ((Convert.ToUInt16(GAMMA_FT2201_Point25_BufdataR[23]) >> 8) << 6));
                        Send_buf[4 + 0x1E] = Convert.ToByte(Convert.ToUInt16(GAMMA_FT2201_Point25_BufdataR[24]) & 0xFF);
                        Send_buf[4 + 0x1F] = Convert.ToByte(Convert.ToUInt16(GAMMA_FT2201_Point25_BufdataR[25]) & 0xFF);
                        Send_buf[4 + 0x20] = Convert.ToByte(Convert.ToUInt16(GAMMA_FT2201_Point25_BufdataR[26]) & 0xFF);
                        Send_buf[4 + 0x21] = Convert.ToByte(((Convert.ToUInt16(GAMMA_FT2201_Point25_BufdataR[24]) >> 8) << 0)
                            + ((Convert.ToUInt16(GAMMA_FT2201_Point25_BufdataR[25]) >> 8) << 2)
                            + ((Convert.ToUInt16(GAMMA_FT2201_Point25_BufdataR[26]) >> 8) << 4));

                        /////G
                        Send_buf[4 + 0x22 + 0x00] = Convert.ToByte(Convert.ToUInt16(GAMMA_FT2201_Point25_BufdataG[0]) & 0xFF);
                        Send_buf[4 + 0x22 + 0x01] = Convert.ToByte(Convert.ToUInt16(GAMMA_FT2201_Point25_BufdataG[1]) & 0xFF);
                        Send_buf[4 + 0x22 + 0x02] = Convert.ToByte(Convert.ToUInt16(GAMMA_FT2201_Point25_BufdataG[2]) & 0xFF);
                        Send_buf[4 + 0x22 + 0x03] = Convert.ToByte(Convert.ToUInt16(GAMMA_FT2201_Point25_BufdataG[3]) & 0xFF);
                        Send_buf[4 + 0x22 + 0x04] = Convert.ToByte(((Convert.ToUInt16(GAMMA_FT2201_Point25_BufdataG[0]) >> 8) << 0)
                            + ((Convert.ToUInt16(GAMMA_FT2201_Point25_BufdataG[1]) >> 8) << 2)
                            + ((Convert.ToUInt16(GAMMA_FT2201_Point25_BufdataG[2]) >> 8) << 4)
                            + ((Convert.ToUInt16(GAMMA_FT2201_Point25_BufdataG[3]) >> 8) << 6));
                        Send_buf[4 + 0x22 + 0x05] = Convert.ToByte(Convert.ToUInt16(GAMMA_FT2201_Point25_BufdataG[4]) & 0xFF);
                        Send_buf[4 + 0x22 + 0x06] = Convert.ToByte(Convert.ToUInt16(GAMMA_FT2201_Point25_BufdataG[5]) & 0xFF);
                        Send_buf[4 + 0x22 + 0x07] = Convert.ToByte(Convert.ToUInt16(GAMMA_FT2201_Point25_BufdataG[6]) & 0xFF);
                        Send_buf[4 + 0x22 + 0x08] = Convert.ToByte(Convert.ToUInt16(GAMMA_FT2201_Point25_BufdataG[7]) & 0xFF);
                        Send_buf[4 + 0x22 + 0x09] = Convert.ToByte(((Convert.ToUInt16(GAMMA_FT2201_Point25_BufdataG[4]) >> 8) << 0)
                            + ((Convert.ToUInt16(GAMMA_FT2201_Point25_BufdataG[5]) >> 8) << 2)
                            + ((Convert.ToUInt16(GAMMA_FT2201_Point25_BufdataG[6]) >> 8) << 4)
                            + ((Convert.ToUInt16(GAMMA_FT2201_Point25_BufdataG[7]) >> 8) << 6));
                        Send_buf[4 + 0x22 + 0x0A] = Convert.ToByte(Convert.ToUInt16(GAMMA_FT2201_Point25_BufdataG[8]) & 0xFF);
                        Send_buf[4 + 0x22 + 0x0B] = Convert.ToByte(Convert.ToUInt16(GAMMA_FT2201_Point25_BufdataG[9]) & 0xFF);
                        Send_buf[4 + 0x22 + 0x0C] = Convert.ToByte(Convert.ToUInt16(GAMMA_FT2201_Point25_BufdataG[10]) & 0xFF);
                        Send_buf[4 + 0x22 + 0x0D] = Convert.ToByte(Convert.ToUInt16(GAMMA_FT2201_Point25_BufdataG[11]) & 0xFF);
                        Send_buf[4 + 0x22 + 0x0E] = Convert.ToByte(((Convert.ToUInt16(GAMMA_FT2201_Point25_BufdataG[8]) >> 8) << 0)
                            + ((Convert.ToUInt16(GAMMA_FT2201_Point25_BufdataG[9]) >> 8) << 2)
                            + ((Convert.ToUInt16(GAMMA_FT2201_Point25_BufdataG[10]) >> 8) << 4)
                            + ((Convert.ToUInt16(GAMMA_FT2201_Point25_BufdataG[11]) >> 8) << 6));
                        Send_buf[4 + 0x22 + 0x0F] = Convert.ToByte(Convert.ToUInt16(GAMMA_FT2201_Point25_BufdataG[12]) & 0xFF);
                        Send_buf[4 + 0x22 + 0x10] = Convert.ToByte(Convert.ToUInt16(GAMMA_FT2201_Point25_BufdataG[13]) & 0xFF);
                        Send_buf[4 + 0x22 + 0x11] = Convert.ToByte(Convert.ToUInt16(GAMMA_FT2201_Point25_BufdataG[14]) & 0xFF);
                        Send_buf[4 + 0x22 + 0x12] = Convert.ToByte(Convert.ToUInt16(GAMMA_FT2201_Point25_BufdataG[15]) & 0xFF);
                        Send_buf[4 + 0x22 + 0x13] = Convert.ToByte(((Convert.ToUInt16(GAMMA_FT2201_Point25_BufdataG[12]) >> 8) << 0)
                            + ((Convert.ToUInt16(GAMMA_FT2201_Point25_BufdataG[13]) >> 8) << 2)
                            + ((Convert.ToUInt16(GAMMA_FT2201_Point25_BufdataG[14]) >> 8) << 4)
                            + ((Convert.ToUInt16(GAMMA_FT2201_Point25_BufdataG[15]) >> 8) << 6));
                        Send_buf[4 + 0x22 + 0x14] = Convert.ToByte(Convert.ToUInt16(GAMMA_FT2201_Point25_BufdataG[16]) & 0xFF);
                        Send_buf[4 + 0x22 + 0x15] = Convert.ToByte(Convert.ToUInt16(GAMMA_FT2201_Point25_BufdataG[17]) & 0xFF);
                        Send_buf[4 + 0x22 + 0x16] = Convert.ToByte(Convert.ToUInt16(GAMMA_FT2201_Point25_BufdataG[18]) & 0xFF);
                        Send_buf[4 + 0x22 + 0x17] = Convert.ToByte(Convert.ToUInt16(GAMMA_FT2201_Point25_BufdataG[19]) & 0xFF);
                        Send_buf[4 + 0x22 + 0x18] = Convert.ToByte(((Convert.ToUInt16(GAMMA_FT2201_Point25_BufdataG[16]) >> 8) << 0)
                            + ((Convert.ToUInt16(GAMMA_FT2201_Point25_BufdataG[17]) >> 8) << 2)
                            + ((Convert.ToUInt16(GAMMA_FT2201_Point25_BufdataG[18]) >> 8) << 4)
                            + ((Convert.ToUInt16(GAMMA_FT2201_Point25_BufdataG[19]) >> 8) << 6));
                        Send_buf[4 + 0x22 + 0x19] = Convert.ToByte(Convert.ToUInt16(GAMMA_FT2201_Point25_BufdataG[20]) & 0xFF);
                        Send_buf[4 + 0x22 + 0x1A] = Convert.ToByte(Convert.ToUInt16(GAMMA_FT2201_Point25_BufdataG[21]) & 0xFF);
                        Send_buf[4 + 0x22 + 0x1B] = Convert.ToByte(Convert.ToUInt16(GAMMA_FT2201_Point25_BufdataG[22]) & 0xFF);
                        Send_buf[4 + 0x22 + 0x1C] = Convert.ToByte(Convert.ToUInt16(GAMMA_FT2201_Point25_BufdataG[23]) & 0xFF);
                        Send_buf[4 + 0x22 + 0x1D] = Convert.ToByte(((Convert.ToUInt16(GAMMA_FT2201_Point25_BufdataG[20]) >> 8) << 0)
                            + ((Convert.ToUInt16(GAMMA_FT2201_Point25_BufdataG[21]) >> 8) << 2)
                            + ((Convert.ToUInt16(GAMMA_FT2201_Point25_BufdataG[22]) >> 8) << 4)
                            + ((Convert.ToUInt16(GAMMA_FT2201_Point25_BufdataG[23]) >> 8) << 6));
                        Send_buf[4 + 0x22 + 0x1E] = Convert.ToByte(Convert.ToUInt16(GAMMA_FT2201_Point25_BufdataG[24]) & 0xFF);
                        Send_buf[4 + 0x22 + 0x1F] = Convert.ToByte(Convert.ToUInt16(GAMMA_FT2201_Point25_BufdataG[25]) & 0xFF);
                        Send_buf[4 + 0x22 + 0x20] = Convert.ToByte(Convert.ToUInt16(GAMMA_FT2201_Point25_BufdataG[26]) & 0xFF);
                        Send_buf[4 + 0x22 + 0x21] = Convert.ToByte(((Convert.ToUInt16(GAMMA_FT2201_Point25_BufdataG[24]) >> 8) << 0)
                            + ((Convert.ToUInt16(GAMMA_FT2201_Point25_BufdataG[25]) >> 8) << 2)
                            + ((Convert.ToUInt16(GAMMA_FT2201_Point25_BufdataG[26]) >> 8) << 4));
                        //Send_buf[4 + 0x34 + 0x34] = Convert.ToByte(Convert.ToUInt16(GAMMA_FT2201_Point25_BufdataG[24]) & 0xFF);

                        //////B
                        Send_buf[4 + 0x44 + 0x00] = Convert.ToByte(Convert.ToUInt16(GAMMA_FT2201_Point25_BufdataB[0]) & 0xFF);
                        Send_buf[4 + 0x44 + 0x01] = Convert.ToByte(Convert.ToUInt16(GAMMA_FT2201_Point25_BufdataB[1]) & 0xFF);
                        Send_buf[4 + 0x44 + 0x02] = Convert.ToByte(Convert.ToUInt16(GAMMA_FT2201_Point25_BufdataB[2]) & 0xFF);
                        Send_buf[4 + 0x44 + 0x03] = Convert.ToByte(Convert.ToUInt16(GAMMA_FT2201_Point25_BufdataB[3]) & 0xFF);
                        Send_buf[4 + 0x44 + 0x04] = Convert.ToByte(((Convert.ToUInt16(GAMMA_FT2201_Point25_BufdataB[0]) >> 8) << 0)
                            + ((Convert.ToUInt16(GAMMA_FT2201_Point25_BufdataB[1]) >> 8) << 2)
                            + ((Convert.ToUInt16(GAMMA_FT2201_Point25_BufdataB[2]) >> 8) << 4)
                            + ((Convert.ToUInt16(GAMMA_FT2201_Point25_BufdataB[3]) >> 8) << 6));
                        Send_buf[4 + 0x44 + 0x05] = Convert.ToByte(Convert.ToUInt16(GAMMA_FT2201_Point25_BufdataB[4]) & 0xFF);
                        Send_buf[4 + 0x44 + 0x06] = Convert.ToByte(Convert.ToUInt16(GAMMA_FT2201_Point25_BufdataB[5]) & 0xFF);
                        Send_buf[4 + 0x44 + 0x07] = Convert.ToByte(Convert.ToUInt16(GAMMA_FT2201_Point25_BufdataB[6]) & 0xFF);
                        Send_buf[4 + 0x44 + 0x08] = Convert.ToByte(Convert.ToUInt16(GAMMA_FT2201_Point25_BufdataB[7]) & 0xFF);
                        Send_buf[4 + 0x44 + 0x09] = Convert.ToByte(((Convert.ToUInt16(GAMMA_FT2201_Point25_BufdataB[4]) >> 8) << 0)
                            + ((Convert.ToUInt16(GAMMA_FT2201_Point25_BufdataB[5]) >> 8) << 2)
                            + ((Convert.ToUInt16(GAMMA_FT2201_Point25_BufdataB[6]) >> 8) << 4)
                            + ((Convert.ToUInt16(GAMMA_FT2201_Point25_BufdataB[7]) >> 8) << 6));
                        Send_buf[4 + 0x44 + 0x0A] = Convert.ToByte(Convert.ToUInt16(GAMMA_FT2201_Point25_BufdataB[8]) & 0xFF);
                        Send_buf[4 + 0x44 + 0x0B] = Convert.ToByte(Convert.ToUInt16(GAMMA_FT2201_Point25_BufdataB[9]) & 0xFF);
                        Send_buf[4 + 0x44 + 0x0C] = Convert.ToByte(Convert.ToUInt16(GAMMA_FT2201_Point25_BufdataB[10]) & 0xFF);
                        Send_buf[4 + 0x44 + 0x0D] = Convert.ToByte(Convert.ToUInt16(GAMMA_FT2201_Point25_BufdataB[11]) & 0xFF);
                        Send_buf[4 + 0x44 + 0x0E] = Convert.ToByte(((Convert.ToUInt16(GAMMA_FT2201_Point25_BufdataB[8]) >> 8) << 0)
                            + ((Convert.ToUInt16(GAMMA_FT2201_Point25_BufdataB[9]) >> 8) << 2)
                            + ((Convert.ToUInt16(GAMMA_FT2201_Point25_BufdataB[10]) >> 8) << 4)
                            + ((Convert.ToUInt16(GAMMA_FT2201_Point25_BufdataB[11]) >> 8) << 6));
                        Send_buf[4 + 0x44 + 0x0F] = Convert.ToByte(Convert.ToUInt16(GAMMA_FT2201_Point25_BufdataB[12]) & 0xFF);
                        Send_buf[4 + 0x44 + 0x10] = Convert.ToByte(Convert.ToUInt16(GAMMA_FT2201_Point25_BufdataB[13]) & 0xFF);
                        Send_buf[4 + 0x44 + 0x11] = Convert.ToByte(Convert.ToUInt16(GAMMA_FT2201_Point25_BufdataB[14]) & 0xFF);
                        Send_buf[4 + 0x44 + 0x12] = Convert.ToByte(Convert.ToUInt16(GAMMA_FT2201_Point25_BufdataB[15]) & 0xFF);
                        Send_buf[4 + 0x44 + 0x13] = Convert.ToByte(((Convert.ToUInt16(GAMMA_FT2201_Point25_BufdataB[12]) >> 8) << 0)
                            + ((Convert.ToUInt16(GAMMA_FT2201_Point25_BufdataB[13]) >> 8) << 2)
                            + ((Convert.ToUInt16(GAMMA_FT2201_Point25_BufdataB[14]) >> 8) << 4)
                            + ((Convert.ToUInt16(GAMMA_FT2201_Point25_BufdataB[15]) >> 8) << 6));
                        Send_buf[4 + 0x44 + 0x14] = Convert.ToByte(Convert.ToUInt16(GAMMA_FT2201_Point25_BufdataB[16]) & 0xFF);
                        Send_buf[4 + 0x44 + 0x15] = Convert.ToByte(Convert.ToUInt16(GAMMA_FT2201_Point25_BufdataB[17]) & 0xFF);
                        Send_buf[4 + 0x44 + 0x16] = Convert.ToByte(Convert.ToUInt16(GAMMA_FT2201_Point25_BufdataB[18]) & 0xFF);
                        Send_buf[4 + 0x44 + 0x17] = Convert.ToByte(Convert.ToUInt16(GAMMA_FT2201_Point25_BufdataB[19]) & 0xFF);
                        Send_buf[4 + 0x44 + 0x18] = Convert.ToByte(((Convert.ToUInt16(GAMMA_FT2201_Point25_BufdataB[16]) >> 8) << 0)
                            + ((Convert.ToUInt16(GAMMA_FT2201_Point25_BufdataB[17]) >> 8) << 2)
                            + ((Convert.ToUInt16(GAMMA_FT2201_Point25_BufdataB[18]) >> 8) << 4)
                            + ((Convert.ToUInt16(GAMMA_FT2201_Point25_BufdataB[19]) >> 8) << 6));
                        Send_buf[4 + 0x44 + 0x19] = Convert.ToByte(Convert.ToUInt16(GAMMA_FT2201_Point25_BufdataB[20]) & 0xFF);
                        Send_buf[4 + 0x44 + 0x1A] = Convert.ToByte(Convert.ToUInt16(GAMMA_FT2201_Point25_BufdataB[21]) & 0xFF);
                        Send_buf[4 + 0x44 + 0x1B] = Convert.ToByte(Convert.ToUInt16(GAMMA_FT2201_Point25_BufdataB[22]) & 0xFF);
                        Send_buf[4 + 0x44 + 0x1C] = Convert.ToByte(Convert.ToUInt16(GAMMA_FT2201_Point25_BufdataB[23]) & 0xFF);
                        Send_buf[4 + 0x44 + 0x1D] = Convert.ToByte(((Convert.ToUInt16(GAMMA_FT2201_Point25_BufdataB[20]) >> 8) << 0)
                            + ((Convert.ToUInt16(GAMMA_FT2201_Point25_BufdataB[21]) >> 8) << 2)
                            + ((Convert.ToUInt16(GAMMA_FT2201_Point25_BufdataB[22]) >> 8) << 4)
                            + ((Convert.ToUInt16(GAMMA_FT2201_Point25_BufdataB[23]) >> 8) << 6));
                        Send_buf[4 + 0x44 + 0x1E] = Convert.ToByte(Convert.ToUInt16(GAMMA_FT2201_Point25_BufdataB[24]) & 0xFF);
                        Send_buf[4 + 0x44 + 0x1F] = Convert.ToByte(Convert.ToUInt16(GAMMA_FT2201_Point25_BufdataB[25]) & 0xFF);
                        Send_buf[4 + 0x44 + 0x20] = Convert.ToByte(Convert.ToUInt16(GAMMA_FT2201_Point25_BufdataB[26]) & 0xFF);
                        Send_buf[4 + 0x44 + 0x21] = Convert.ToByte(((Convert.ToUInt16(GAMMA_FT2201_Point25_BufdataB[24]) >> 8) << 0)
                            + ((Convert.ToUInt16(GAMMA_FT2201_Point25_BufdataB[25]) >> 8) << 2)
                            + ((Convert.ToUInt16(GAMMA_FT2201_Point25_BufdataB[26]) >> 8) << 4));
                        //Send_buf[4 + 0x69 + 0x31] = Convert.ToByte(Convert.ToUInt16(GAMMA_FT2201_Point25_BufdataB[24]) & 0xFF);


                        if (FT2201_GAMMA_2.Checked == true)
                        {
                            str = str + "E8" + "," + "\r\n";
                        }
                        else
                        {
                            str = str + "C8" + "," + "\r\n";
                        }

                        for (int i = 4; i < 0x66; i++)
                        {

                            str = str + (Send_buf[i].ToString("X2")) + "," + "\r\n";
                        }

                        str = str + "ok";

                        sw.WriteLine(str);
                    }
                    {

                    }
                    sw.Dispose(); //释放资源
                    sw.Close(); //关闭数据流
                    MessageBox.Show("Gamma保存OK！", "系统提示：");
                }
                catch
                {
                    MessageBox.Show("Gamma保存时发生错误，请再试！", "系统提示：");
                }
            }
        }

        private void FT2201_FormClosing(object sender, FormClosingEventArgs e)
        {
            e.Cancel = true;
            btnBack_Click(btnBack, e);
        }

        private void FT2201_VisibleChanged(object sender, EventArgs e)
        {
            if (this.Visible == true) //子窗体已开启标识
            {
                //MainForm.comPort.Close();
                //Thread.Sleep(5);
                MainForm.comPort.DataReceived -= new SerialDataReceivedEventHandler(MainForm.SerialPort1_DataReceived);

                Thread.Sleep(5);
                MainForm.comPort.DataReceived += new SerialDataReceivedEventHandler(SerialPort1_DataReceived);
                Thread.Sleep(5);
                //MainForm.comPort.Open();
                textBox1.Text += "com 已经连接！\r\n";
            }
            else
            {

            }
        }

        private void btnAutogamma310_Click(object sender, EventArgs e)
        {
            int flag = 1;
            double Lr_d, Lg_d, Lb_d;
            D_lums = new double[27, 3];
            D_lum = new double[27];
            index = 0;
            adjust_Rnum = 0;
            adjust_Gnum = 0;
            adjust_Bnum = 0;
            adjust_l = 0;
            adjust_x = 0;
            adjust_y = 0;

            //------------flexible gamma use------------------
            Gamma_auto_adjust_count = 0;
            Gamma_adjust_timeout_flag = false;
            //------------------------------------------------
            D_lum[0] = Convert.ToDouble(text_gray_num0.Text);
            D_lum[1] = Convert.ToDouble(text_gray_num1.Text);
            D_lum[2] = Convert.ToDouble(text_gray_num2.Text);
            D_lum[3] = Convert.ToDouble(text_gray_num3.Text);
            D_lum[4] = Convert.ToDouble(text_gray_num4.Text);
            D_lum[5] = Convert.ToDouble(text_gray_num5.Text);
            D_lum[6] = Convert.ToDouble(text_gray_num6.Text);
            D_lum[7] = Convert.ToDouble(text_gray_num7.Text);
            D_lum[8] = Convert.ToDouble(text_gray_num8.Text);
            D_lum[9] = Convert.ToDouble(text_gray_num9.Text);
            D_lum[10] = Convert.ToDouble(text_gray_num10.Text);
            D_lum[11] = Convert.ToDouble(text_gray_num11.Text);
            D_lum[12] = Convert.ToDouble(text_gray_num12.Text);
            D_lum[13] = Convert.ToDouble(text_gray_num13.Text);
            D_lum[14] = Convert.ToDouble(text_gray_num14.Text);
            D_lum[15] = Convert.ToDouble(text_gray_num15.Text);
            D_lum[16] = Convert.ToDouble(text_gray_num16.Text);
            D_lum[17] = Convert.ToDouble(text_gray_num17.Text);
            D_lum[18] = Convert.ToDouble(text_gray_num18.Text);
            D_lum[19] = Convert.ToDouble(text_gray_num19.Text);
            D_lum[20] = Convert.ToDouble(text_gray_num20.Text);
            D_lum[21] = Convert.ToDouble(text_gray_num21.Text);
            D_lum[22] = Convert.ToDouble(text_gray_num22.Text);
            D_lum[23] = Convert.ToDouble(text_gray_num23.Text);
            D_lum[24] = Convert.ToDouble(text_gray_num24.Text);
            D_lum[25] = Convert.ToDouble(text_gray_num25.Text);
            D_lum[26] = Convert.ToDouble(text_gray_num26.Text);


            D_x = Convert.ToDouble(textNTx.Text);
            D_y = Convert.ToDouble(textNTy.Text);
            Lr_d = R_y * ((D_y - G_y) * (B_x - D_x) + (D_x - G_x) * (D_y - B_y)) / B_y / ((D_y - G_y) * (D_x - R_x) + (D_x - G_x) * (R_y - D_y));
            Lb_d = 1.0;
            Lg_d = G_y * ((D_y - R_y) * (B_x - D_x) + (D_x - R_x) * (D_y - B_y)) / B_y / ((D_y - R_y) * (D_x - G_x) + (D_x - R_x) * (G_y - D_y));

            for (int i = 0; i < 25; i++)
            {
                D_lums[i, 0] = D_lum[i] * Lr_d / (Lr_d + Lg_d + Lb_d);
                D_lums[i, 1] = D_lum[i] * Lg_d / (Lr_d + Lg_d + Lb_d);
                D_lums[i, 2] = D_lum[i] * Lb_d / (Lr_d + Lg_d + Lb_d);
            }



            gamma_r = GAMMAs.FT2201_Gamma_r;
            gamma_g = GAMMAs.FT2201_Gamma_g;
            gamma_b = GAMMAs.FT2201_Gamma_b;



            switch (flag)
            {
                case 1://寄存器值与亮度正比
                    {
                        lum_off = Convert.ToDouble(textNTlum_off.Text);
                        x_off = Convert.ToDouble(textNTx_off.Text);
                        y_off = Convert.ToDouble(textNTy_off.Text);
                        OLED_Show_Click(btnwhite, e);
                        T5.Enabled = true;
                    }
                    break;
                case 2://寄存器值与亮度反比
                    break;
                default:
                    break;
            }
        }

        private void T5_Tick(object sender, EventArgs e)
        {
            double Lr_t, Lg_t, Lb_t, lumr = 0, lumg = 0, lumb = 0;
            Msr_CA310();

            if ((auto_lum < (D_lum[index] + lum_off)) && (auto_lum > (D_lum[index] - lum_off)))//G
            {
                flag1 = 0;
            }
            if ((auto_y < (D_y + y_off)) && (auto_y > (D_y - y_off)) && flag1 == 0)//B
            {
                flag2 = 0;
            }
            if ((auto_x < (D_x + x_off)) && (auto_x > (D_x - x_off)) && flag1 == 0 && flag2 == 0)//R
            {
                flag3 = 0;
            }

            if (flag1 == 0 && flag2 == 0 && flag3 == 0)
            {
                T5.Enabled = false;
                //reset-----
                Gamma_adjust_timeout_flag = false;
                Gamma_auto_adjust_count = 0;
                //--------
                index++;
                adjust_Rnum = 0;
                adjust_Gnum = 0;
                adjust_Bnum = 0;
                adjust_l = 0;
                adjust_x = 0;
                adjust_y = 0;
                if (index == 25)
                {
                    FT2201_Gamma_Click(FT2201_GammaW_all, null);
                    textBox1.Text = textBox1.Text + "FT2201 auto gamma调试完毕!\r\n";
                }
                if (index <= 26)
                {
                    switch (index)
                    {
                        case 1:
                            lum_off = Convert.ToDouble(text_off1.Text);
                            break;
                        case 2:
                            lum_off = Convert.ToDouble(text_off2.Text);
                            break;
                        case 3:
                            lum_off = Convert.ToDouble(text_off3.Text);
                            break;
                        case 4:
                            lum_off = Convert.ToDouble(text_off4.Text);
                            break;
                        case 5:
                            lum_off = Convert.ToDouble(text_off5.Text);
                            break;
                        case 6:
                            lum_off = Convert.ToDouble(text_off6.Text);
                            break;
                        case 7:
                            lum_off = Convert.ToDouble(text_off7.Text);
                            break;
                        case 8:
                            lum_off = Convert.ToDouble(text_off8.Text) * Convert.ToDouble(text_off8.Text) / 100;
                            break;
                        case 9:
                            lum_off = Convert.ToDouble(text_off9.Text) * Convert.ToDouble(text_off9.Text) / 100;
                            break;
                        case 10:
                            lum_off = Convert.ToDouble(text_off10.Text) * Convert.ToDouble(text_off10.Text) / 100;
                            break;
                        case 11:
                            lum_off = Convert.ToDouble(text_off11.Text) * Convert.ToDouble(text_off11.Text) / 100;
                            break;
                        case 12:
                            lum_off = Convert.ToDouble(text_off12.Text) * Convert.ToDouble(text_off12.Text) / 100;
                            break;
                        case 13:
                            lum_off = Convert.ToDouble(text_off13.Text) * Convert.ToDouble(text_off13.Text) / 100;
                            break;
                        case 14:
                            lum_off = Convert.ToDouble(text_off14.Text) * Convert.ToDouble(text_off14.Text) / 100;
                            break;
                        case 15:
                            lum_off = Convert.ToDouble(text_off15.Text) * Convert.ToDouble(text_off15.Text) / 100;
                            break;
                        case 16:
                            lum_off = Convert.ToDouble(text_off16.Text) * Convert.ToDouble(text_off16.Text) / 100;
                            break;
                        case 17:
                            lum_off = Convert.ToDouble(text_off17.Text) * Convert.ToDouble(text_off17.Text) / 100;
                            break;
                        case 18:
                            lum_off = Convert.ToDouble(text_off18.Text) * Convert.ToDouble(text_off18.Text) / 100;
                            break;
                        case 19:
                            lum_off = Convert.ToDouble(text_off19.Text) * Convert.ToDouble(text_off19.Text) / 100;
                            break;
                        case 20:
                            lum_off = Convert.ToDouble(text_off20.Text) * Convert.ToDouble(text_off20.Text) / 100;
                            break;
                        case 21:
                            lum_off = Convert.ToDouble(text_off21.Text) * Convert.ToDouble(text_off21.Text) / 100;
                            break;
                        case 22:
                            lum_off = Convert.ToDouble(text_off22.Text) * Convert.ToDouble(text_off22.Text) / 100;
                            break;
                        case 23:
                            lum_off = Convert.ToDouble(text_off23.Text) * Convert.ToDouble(text_off23.Text) / 100;
                            break;
                        case 24:
                            lum_off = Convert.ToDouble(text_off24.Text) * Convert.ToDouble(text_off24.Text) / 100;
                            break;
                        case 25:
                            lum_off = Convert.ToDouble(text_off25.Text) * Convert.ToDouble(text_off25.Text) / 100;
                            break;
                        case 26:
                            lum_off = Convert.ToDouble(text_off26.Text) * Convert.ToDouble(text_off26.Text) / 100;
                            break;
                        default:
                            break;

                    }

                    numPatternR.Value = Parttern_FT2201[index];
                    numPatternG.Value = Parttern_FT2201[index];
                    numPatternB.Value = Parttern_FT2201[index];
                    OLED_Show_Click(btnPatternW, e);//写入Pattern参数
                    Thread.Sleep(25); //1秒=1000毫秒，
                    T5.Enabled = true;
                    flag1 = 1;
                    flag2 = 1;
                    flag3 = 1;
                }
            }
            else
            {
                decimal r, g, b;
                int flexible_gamma_count = 100;

                Lr_t = R_y * ((auto_y - G_y) * (B_x - auto_x) + (auto_x - G_x) * (auto_y - B_y)) / B_y / ((auto_y - G_y) * (auto_x - R_x) + (auto_x - G_x) * (R_y - auto_y));
                Lb_t = 1.0;
                Lg_t = G_y * ((auto_y - R_y) * (B_x - auto_x) + (auto_x - R_x) * (auto_y - B_y)) / B_y / ((auto_y - R_y) * (auto_x - G_x) + (auto_x - R_x) * (G_y - auto_y));
                lumr = auto_lum * Lr_t / (Lr_t + Lg_t + Lb_t);
                lumg = auto_lum * Lg_t / (Lr_t + Lg_t + Lb_t);
                lumb = auto_lum * Lb_t / (Lr_t + Lg_t + Lb_t);

                try
                {
                    switch (index)
                    {
                        case 0:
                            {
                                R6645x_Gamma_auto_Adujst(flag1, flag3, flag2, 50, 10, 1, lumg, lumr, lumb, D_lums[index, 1], D_lums[index, 0], D_lums[index, 2], flexible_gamma_count, FT2201_gammaR26.Value, FT2201_gammaG26.Value, FT2201_gammaB26.Value, 255, 255, 255, 0x3ff, 0x3ff, 0x3ff, 0x3ff, 0x3ff, 0x3ff, out r, out g, out b, out Gamma_adjust_timeout_flag);
                                if (Gamma_adjust_timeout_flag == true)
                                {
                                    flag1 = 0; flag3 = 0; flag2 = 0;
                                }
                                FT2201_gammaR26.Value = r;
                                FT2201_gammaG26.Value = g;
                                FT2201_gammaB26.Value = b;
                                auto_gamma_write(gamma_r[26], Convert.ToUInt16(FT2201_gammaR26.Value), gamma_g[26], Convert.ToUInt16(FT2201_gammaG26.Value), gamma_b[26], Convert.ToUInt16(FT2201_gammaB26.Value));
                            }
                            break;
                        case 1:
                            {
                                R6645x_Gamma_auto_Adujst(flag1, flag3, flag2, 50, 10, 1, lumg, lumr, lumb, D_lums[index, 1], D_lums[index, 0], D_lums[index, 2], flexible_gamma_count, FT2201_gammaR25.Value, FT2201_gammaG25.Value, FT2201_gammaB25.Value, 255, 255, 254, 0x3ff, 0x3ff, 0x3ff, 0x3ff, 0x3ff, 0x3ff, out r, out g, out b, out Gamma_adjust_timeout_flag);
                                if (Gamma_adjust_timeout_flag == true)
                                {
                                    flag1 = 0; flag3 = 0; flag2 = 0;
                                }
                                FT2201_gammaR25.Value = r;
                                FT2201_gammaG25.Value = g;
                                FT2201_gammaB25.Value = b;
                                auto_gamma_write(gamma_r[25], Convert.ToUInt16(FT2201_gammaR25.Value), gamma_g[25], Convert.ToUInt16(FT2201_gammaG25.Value), gamma_b[25], Convert.ToUInt16(FT2201_gammaB25.Value));

                            }
                            break;
                        case 2:
                            {
                                R6645x_Gamma_auto_Adujst(flag1, flag3, flag2, 50, 10, 1, lumg, lumr, lumb, D_lums[index, 1], D_lums[index, 0], D_lums[index, 2], flexible_gamma_count, FT2201_gammaR24.Value, FT2201_gammaG24.Value, FT2201_gammaB24.Value, 255, 254, 252, FT2201_gammaR26.Value, FT2201_gammaG26.Value, FT2201_gammaB26.Value, FT2201_gammaR25.Value, FT2201_gammaG25.Value, FT2201_gammaB25.Value, out r, out g, out b, out Gamma_adjust_timeout_flag);
                                if (Gamma_adjust_timeout_flag == true)
                                {
                                    flag1 = 0; flag3 = 0; flag2 = 0;
                                }
                                FT2201_gammaR24.Value = r;
                                FT2201_gammaG24.Value = g;
                                FT2201_gammaB24.Value = b;
                                auto_gamma_write(gamma_r[24], Convert.ToUInt16(FT2201_gammaR24.Value), gamma_g[24], Convert.ToUInt16(FT2201_gammaG24.Value), gamma_b[24], Convert.ToUInt16(FT2201_gammaB24.Value));

                            }
                            break;
                        case 3:
                            {
                                R6645x_Gamma_auto_Adujst(flag1, flag3, flag2, 40, 10, 1, lumg, lumr, lumb, D_lums[index, 1], D_lums[index, 0], D_lums[index, 2], flexible_gamma_count, FT2201_gammaR23.Value, FT2201_gammaG23.Value, FT2201_gammaB23.Value, 254, 252, 248, FT2201_gammaR25.Value, FT2201_gammaG25.Value, FT2201_gammaB25.Value, FT2201_gammaR24.Value, FT2201_gammaG24.Value, FT2201_gammaB24.Value, out r, out g, out b, out Gamma_adjust_timeout_flag);
                                if (Gamma_adjust_timeout_flag == true)
                                {
                                    flag1 = 0; flag3 = 0; flag2 = 0;
                                }
                                FT2201_gammaR23.Value = r;
                                FT2201_gammaG23.Value = g;
                                FT2201_gammaB23.Value = b;
                                auto_gamma_write(gamma_r[23], Convert.ToUInt16(FT2201_gammaR23.Value), gamma_g[23], Convert.ToUInt16(FT2201_gammaG23.Value), gamma_b[23], Convert.ToUInt16(FT2201_gammaB23.Value));

                            }
                            break;
                        case 4:
                            {
                                R6645x_Gamma_auto_Adujst(flag1, flag3, flag2, 40, 10, 1, lumg, lumr, lumb, D_lums[index, 1], D_lums[index, 0], D_lums[index, 2], flexible_gamma_count, FT2201_gammaR22.Value, FT2201_gammaG22.Value, FT2201_gammaB22.Value, 252, 248, 240, FT2201_gammaR24.Value, FT2201_gammaG24.Value, FT2201_gammaB24.Value, FT2201_gammaR23.Value, FT2201_gammaG23.Value, FT2201_gammaB23.Value, out r, out g, out b, out Gamma_adjust_timeout_flag);
                                if (Gamma_adjust_timeout_flag == true)
                                {
                                    flag1 = 0; flag3 = 0; flag2 = 0;
                                }
                                FT2201_gammaR22.Value = r;
                                FT2201_gammaG22.Value = g;
                                FT2201_gammaB22.Value = b;
                                auto_gamma_write(gamma_r[22], Convert.ToUInt16(FT2201_gammaR22.Value), gamma_g[22], Convert.ToUInt16(FT2201_gammaG22.Value), gamma_b[22], Convert.ToUInt16(FT2201_gammaB22.Value));

                            }
                            break;
                        case 5:
                            {
                                R6645x_Gamma_auto_Adujst(flag1, flag3, flag2, 40, 5, 1, lumg, lumr, lumb, D_lums[index, 1], D_lums[index, 0], D_lums[index, 2], flexible_gamma_count, FT2201_gammaR21.Value, FT2201_gammaG21.Value, FT2201_gammaB21.Value, 248, 240, 232, FT2201_gammaR23.Value, FT2201_gammaG23.Value, FT2201_gammaB23.Value, FT2201_gammaR22.Value, FT2201_gammaG22.Value, FT2201_gammaB22.Value, out r, out g, out b, out Gamma_adjust_timeout_flag);
                                if (Gamma_adjust_timeout_flag == true)
                                {
                                    flag1 = 0; flag3 = 0; flag2 = 0;
                                }
                                FT2201_gammaR21.Value = r;
                                FT2201_gammaG21.Value = g;
                                FT2201_gammaB21.Value = b;
                                auto_gamma_write(gamma_r[21], Convert.ToUInt16(FT2201_gammaR21.Value), gamma_g[21], Convert.ToUInt16(FT2201_gammaG21.Value), gamma_b[21], Convert.ToUInt16(FT2201_gammaB21.Value));


                            }
                            break;
                        case 6:
                            {
                                R6645x_Gamma_auto_Adujst(flag1, flag3, flag2, 40, 5, 1, lumg, lumr, lumb, D_lums[index, 1], D_lums[index, 0], D_lums[index, 2], flexible_gamma_count, FT2201_gammaR20.Value, FT2201_gammaG20.Value, FT2201_gammaB20.Value, 240, 232, 224, FT2201_gammaR22.Value, FT2201_gammaG22.Value, FT2201_gammaB22.Value, FT2201_gammaR21.Value, FT2201_gammaG21.Value, FT2201_gammaB21.Value, out r, out g, out b, out Gamma_adjust_timeout_flag);
                                if (Gamma_adjust_timeout_flag == true)
                                {
                                    flag1 = 0; flag3 = 0; flag2 = 0;
                                }
                                FT2201_gammaR20.Value = r;
                                FT2201_gammaG20.Value = g;
                                FT2201_gammaB20.Value = b;
                                auto_gamma_write(gamma_r[20], Convert.ToUInt16(FT2201_gammaR20.Value), gamma_g[20], Convert.ToUInt16(FT2201_gammaG20.Value), gamma_b[20], Convert.ToUInt16(FT2201_gammaB20.Value));

                            }
                            break;
                        case 7:
                            {
                                R6645x_Gamma_auto_Adujst(flag1, flag3, flag2, 40, 5, 1, lumg, lumr, lumb, D_lums[index, 1], D_lums[index, 0], D_lums[index, 2], flexible_gamma_count, FT2201_gammaR19.Value, FT2201_gammaG19.Value, FT2201_gammaB19.Value, 232, 224, 218, FT2201_gammaR21.Value, FT2201_gammaG21.Value, FT2201_gammaB21.Value, FT2201_gammaR20.Value, FT2201_gammaG20.Value, FT2201_gammaB20.Value, out r, out g, out b, out Gamma_adjust_timeout_flag);
                                if (Gamma_adjust_timeout_flag == true)
                                {
                                    flag1 = 0; flag3 = 0; flag2 = 0;
                                }
                                FT2201_gammaR19.Value = r;
                                FT2201_gammaG19.Value = g;
                                FT2201_gammaB19.Value = b;
                                auto_gamma_write(gamma_r[19], Convert.ToUInt16(FT2201_gammaR19.Value), gamma_g[19], Convert.ToUInt16(FT2201_gammaG19.Value), gamma_b[19], Convert.ToUInt16(FT2201_gammaB19.Value));

                            }
                            break;
                        case 8:
                            {
                                R6645x_Gamma_auto_Adujst(flag1, flag3, flag2, 40, 5, 1, lumg, lumr, lumb, D_lums[index, 1], D_lums[index, 0], D_lums[index, 2], flexible_gamma_count, FT2201_gammaR18.Value, FT2201_gammaG18.Value, FT2201_gammaB18.Value, 224, 218, 208, FT2201_gammaR20.Value, FT2201_gammaG20.Value, FT2201_gammaB20.Value, FT2201_gammaR19.Value, FT2201_gammaG19.Value, FT2201_gammaB19.Value, out r, out g, out b, out Gamma_adjust_timeout_flag);
                                if (Gamma_adjust_timeout_flag == true)
                                {
                                    flag1 = 0; flag3 = 0; flag2 = 0;
                                }
                                FT2201_gammaR18.Value = r;
                                FT2201_gammaG18.Value = g;
                                FT2201_gammaB18.Value = b;
                                auto_gamma_write(gamma_r[18], Convert.ToUInt16(FT2201_gammaR18.Value), gamma_g[18], Convert.ToUInt16(FT2201_gammaG18.Value), gamma_b[18], Convert.ToUInt16(FT2201_gammaB18.Value));

                            }
                            break;
                        case 9:
                            {
                                R6645x_Gamma_auto_Adujst(flag1, flag3, flag2, 40, 5, 1, lumg, lumr, lumb, D_lums[index, 1], D_lums[index, 0], D_lums[index, 2], flexible_gamma_count, FT2201_gammaR17.Value, FT2201_gammaG17.Value, FT2201_gammaB17.Value, 218, 208, 192, FT2201_gammaR19.Value, FT2201_gammaG19.Value, FT2201_gammaB19.Value, FT2201_gammaR18.Value, FT2201_gammaG18.Value, FT2201_gammaB18.Value, out r, out g, out b, out Gamma_adjust_timeout_flag);
                                if (Gamma_adjust_timeout_flag == true)
                                {
                                    flag1 = 0; flag3 = 0; flag2 = 0;
                                }
                                FT2201_gammaR17.Value = r;
                                FT2201_gammaG17.Value = g;
                                FT2201_gammaB17.Value = b;
                                auto_gamma_write(gamma_r[17], Convert.ToUInt16(FT2201_gammaR17.Value), gamma_g[17], Convert.ToUInt16(FT2201_gammaG17.Value), gamma_b[17], Convert.ToUInt16(FT2201_gammaB17.Value));

                            }
                            break;
                        case 10:
                            {
                                R6645x_Gamma_auto_Adujst(flag1, flag3, flag2, 40, 5, 1, lumg, lumr, lumb, D_lums[index, 1], D_lums[index, 0], D_lums[index, 2], flexible_gamma_count, FT2201_gammaR16.Value, FT2201_gammaG16.Value, FT2201_gammaB16.Value, 208, 192, 176, FT2201_gammaR18.Value, FT2201_gammaG18.Value, FT2201_gammaB18.Value, FT2201_gammaR17.Value, FT2201_gammaG17.Value, FT2201_gammaB17.Value, out r, out g, out b, out Gamma_adjust_timeout_flag);
                                if (Gamma_adjust_timeout_flag == true)
                                {
                                    flag1 = 0; flag3 = 0; flag2 = 0;
                                }
                                FT2201_gammaR16.Value = r;
                                FT2201_gammaG16.Value = g;
                                FT2201_gammaB16.Value = b;
                                auto_gamma_write(gamma_r[16], Convert.ToUInt16(FT2201_gammaR16.Value), gamma_g[16], Convert.ToUInt16(FT2201_gammaG16.Value), gamma_b[16], Convert.ToUInt16(FT2201_gammaB16.Value));

                            }
                            break;
                        case 11:
                            {
                                R6645x_Gamma_auto_Adujst(flag1, flag3, flag2, 40, 5, 1, lumg, lumr, lumb, D_lums[index, 1], D_lums[index, 0], D_lums[index, 2], flexible_gamma_count, FT2201_gammaR15.Value, FT2201_gammaG15.Value, FT2201_gammaB15.Value, 192, 176, 160, FT2201_gammaR17.Value, FT2201_gammaG17.Value, FT2201_gammaB17.Value, FT2201_gammaR16.Value, FT2201_gammaG16.Value, FT2201_gammaB16.Value, out r, out g, out b, out Gamma_adjust_timeout_flag);
                                if (Gamma_adjust_timeout_flag == true)
                                {
                                    flag1 = 0; flag3 = 0; flag2 = 0;
                                }
                                FT2201_gammaR15.Value = r;
                                FT2201_gammaG15.Value = g;
                                FT2201_gammaB15.Value = b;
                                auto_gamma_write(gamma_r[15], Convert.ToUInt16(FT2201_gammaR15.Value), gamma_g[15], Convert.ToUInt16(FT2201_gammaG15.Value), gamma_b[15], Convert.ToUInt16(FT2201_gammaB15.Value));

                            }
                            break;
                        case 12:
                            {
                                R6645x_Gamma_auto_Adujst(flag1, flag3, flag2, 40, 5, 1, lumg, lumr, lumb, D_lums[index, 1], D_lums[index, 0], D_lums[index, 2], flexible_gamma_count, FT2201_gammaR14.Value, FT2201_gammaG14.Value, FT2201_gammaB14.Value, 176, 160, 144, FT2201_gammaR16.Value, FT2201_gammaG16.Value, FT2201_gammaB16.Value, FT2201_gammaR15.Value, FT2201_gammaG15.Value, FT2201_gammaB15.Value, out r, out g, out b, out Gamma_adjust_timeout_flag);
                                if (Gamma_adjust_timeout_flag == true)
                                {
                                    flag1 = 0; flag3 = 0; flag2 = 0;
                                }
                                FT2201_gammaR14.Value = r;
                                FT2201_gammaG14.Value = g;
                                FT2201_gammaB14.Value = b;
                                auto_gamma_write(gamma_r[14], Convert.ToUInt16(FT2201_gammaR14.Value), gamma_g[14], Convert.ToUInt16(FT2201_gammaG14.Value), gamma_b[14], Convert.ToUInt16(FT2201_gammaB14.Value));

                            }
                            break;
                        case 13:
                            {
                                R6645x_Gamma_auto_Adujst(flag1, flag3, flag2, 40, 5, 1, lumg, lumr, lumb, D_lums[index, 1], D_lums[index, 0], D_lums[index, 2], flexible_gamma_count, FT2201_gammaR13.Value, FT2201_gammaG13.Value, FT2201_gammaB13.Value, 160, 144, 128, FT2201_gammaR15.Value, FT2201_gammaG15.Value, FT2201_gammaB15.Value, FT2201_gammaR14.Value, FT2201_gammaG14.Value, FT2201_gammaB14.Value, out r, out g, out b, out Gamma_adjust_timeout_flag);
                                if (Gamma_adjust_timeout_flag == true)
                                {
                                    flag1 = 0; flag3 = 0; flag2 = 0;
                                }
                                FT2201_gammaR13.Value = r;
                                FT2201_gammaG13.Value = g;
                                FT2201_gammaB13.Value = b;
                                auto_gamma_write(gamma_r[13], Convert.ToUInt16(FT2201_gammaR13.Value), gamma_g[13], Convert.ToUInt16(FT2201_gammaG13.Value), gamma_b[13], Convert.ToUInt16(FT2201_gammaB13.Value));

                            }
                            break;
                        case 14:
                            {
                                R6645x_Gamma_auto_Adujst(flag1, flag3, flag2, 40, 5, 1, lumg, lumr, lumb, D_lums[index, 1], D_lums[index, 0], D_lums[index, 2], flexible_gamma_count, FT2201_gammaR12.Value, FT2201_gammaG12.Value, FT2201_gammaB12.Value, 144, 128, 112, FT2201_gammaR14.Value, FT2201_gammaG14.Value, FT2201_gammaB14.Value, FT2201_gammaR13.Value, FT2201_gammaG13.Value, FT2201_gammaB13.Value, out r, out g, out b, out Gamma_adjust_timeout_flag);
                                if (Gamma_adjust_timeout_flag == true)
                                {
                                    flag1 = 0; flag3 = 0; flag2 = 0;
                                }
                                FT2201_gammaR12.Value = r;
                                FT2201_gammaG12.Value = g;
                                FT2201_gammaB12.Value = b;
                                auto_gamma_write(gamma_r[12], Convert.ToUInt16(FT2201_gammaR12.Value), gamma_g[12], Convert.ToUInt16(FT2201_gammaG12.Value), gamma_b[12], Convert.ToUInt16(FT2201_gammaB12.Value));

                            }
                            break;
                        case 15:
                            {
                                R6645x_Gamma_auto_Adujst(flag1, flag3, flag2, 40, 5, 1, lumg, lumr, lumb, D_lums[index, 1], D_lums[index, 0], D_lums[index, 2], flexible_gamma_count, FT2201_gammaR11.Value, FT2201_gammaG11.Value, FT2201_gammaB11.Value, 128, 112, 96, FT2201_gammaR13.Value, FT2201_gammaG13.Value, FT2201_gammaB13.Value, FT2201_gammaR12.Value, FT2201_gammaG12.Value, FT2201_gammaB12.Value, out r, out g, out b, out Gamma_adjust_timeout_flag);
                                if (Gamma_adjust_timeout_flag == true)
                                {
                                    flag1 = 0; flag3 = 0; flag2 = 0;
                                }
                                FT2201_gammaR11.Value = r;
                                FT2201_gammaG11.Value = g;
                                FT2201_gammaB11.Value = b;
                                auto_gamma_write(gamma_r[11], Convert.ToUInt16(FT2201_gammaR11.Value), gamma_g[11], Convert.ToUInt16(FT2201_gammaG11.Value), gamma_b[11], Convert.ToUInt16(FT2201_gammaB11.Value));

                            }
                            break;
                        case 16:
                            {
                                R6645x_Gamma_auto_Adujst(flag1, flag3, flag2, 40, 4, 1, lumg, lumr, lumb, D_lums[index, 1], D_lums[index, 0], D_lums[index, 2], flexible_gamma_count, FT2201_gammaR10.Value, FT2201_gammaG10.Value, FT2201_gammaB10.Value, 112, 96, 64, FT2201_gammaR12.Value, FT2201_gammaG12.Value, FT2201_gammaB12.Value, FT2201_gammaR11.Value, FT2201_gammaG11.Value, FT2201_gammaB11.Value, out r, out g, out b, out Gamma_adjust_timeout_flag);
                                if (Gamma_adjust_timeout_flag == true)
                                {
                                    flag1 = 0; flag3 = 0; flag2 = 0;
                                }
                                FT2201_gammaR10.Value = r;
                                FT2201_gammaG10.Value = g;
                                FT2201_gammaB10.Value = b;
                                auto_gamma_write(gamma_r[10], Convert.ToUInt16(FT2201_gammaR10.Value), gamma_g[10], Convert.ToUInt16(FT2201_gammaG10.Value), gamma_b[10], Convert.ToUInt16(FT2201_gammaB10.Value));

                            }
                            break;
                        case 17:
                            {
                                R6645x_Gamma_auto_Adujst(flag1, flag3, flag2, 40, 4, 1, lumg, lumr, lumb, D_lums[index, 1], D_lums[index, 0], D_lums[index, 2], flexible_gamma_count, FT2201_gammaR9.Value, FT2201_gammaG9.Value, FT2201_gammaB9.Value, 96, 64, 48, FT2201_gammaR11.Value, FT2201_gammaG11.Value, FT2201_gammaB11.Value, FT2201_gammaR10.Value, FT2201_gammaG10.Value, FT2201_gammaB10.Value, out r, out g, out b, out Gamma_adjust_timeout_flag);
                                if (Gamma_adjust_timeout_flag == true)
                                {
                                    flag1 = 0; flag3 = 0; flag2 = 0;
                                }
                                FT2201_gammaR9.Value = r;
                                FT2201_gammaG9.Value = g;
                                FT2201_gammaB9.Value = b;
                                auto_gamma_write(gamma_r[9], Convert.ToUInt16(FT2201_gammaR9.Value), gamma_g[9], Convert.ToUInt16(FT2201_gammaG9.Value), gamma_b[9], Convert.ToUInt16(FT2201_gammaB9.Value));

                            }
                            break;
                        case 18:
                            {
                                R6645x_Gamma_auto_Adujst(flag1, flag3, flag2, 30, 3, 1, lumg, lumr, lumb, D_lums[index, 1], D_lums[index, 0], D_lums[index, 2], flexible_gamma_count, FT2201_gammaR8.Value, FT2201_gammaG8.Value, FT2201_gammaB8.Value, 64, 48, 40, FT2201_gammaR10.Value, FT2201_gammaG10.Value, FT2201_gammaB10.Value, FT2201_gammaR9.Value, FT2201_gammaG9.Value, FT2201_gammaB9.Value, out r, out g, out b, out Gamma_adjust_timeout_flag);
                                if (Gamma_adjust_timeout_flag == true)
                                {
                                    flag1 = 0; flag3 = 0; flag2 = 0;
                                }
                                FT2201_gammaR8.Value = r;
                                FT2201_gammaG8.Value = g;
                                FT2201_gammaB8.Value = b;
                                auto_gamma_write(gamma_r[8], Convert.ToUInt16(FT2201_gammaR8.Value), gamma_g[8], Convert.ToUInt16(FT2201_gammaG8.Value), gamma_b[8], Convert.ToUInt16(FT2201_gammaB8.Value));

                            }
                            break;
                        case 19:
                            {
                                R6645x_Gamma_auto_Adujst(flag1, flag3, flag2, 20, 3, 1, lumg, lumr, lumb, D_lums[index, 1], D_lums[index, 0], D_lums[index, 2], flexible_gamma_count, FT2201_gammaR7.Value, FT2201_gammaG7.Value, FT2201_gammaB7.Value, 48, 40, 32, FT2201_gammaR9.Value, FT2201_gammaG9.Value, FT2201_gammaB9.Value, FT2201_gammaR8.Value, FT2201_gammaG8.Value, FT2201_gammaB8.Value, out r, out g, out b, out Gamma_adjust_timeout_flag);
                                if (Gamma_adjust_timeout_flag == true)
                                {
                                    flag1 = 0; flag3 = 0; flag2 = 0;
                                }
                                FT2201_gammaR7.Value = r;
                                FT2201_gammaG7.Value = g;
                                FT2201_gammaB7.Value = b;
                                auto_gamma_write(gamma_r[7], Convert.ToUInt16(FT2201_gammaR7.Value), gamma_g[7], Convert.ToUInt16(FT2201_gammaG7.Value), gamma_b[7], Convert.ToUInt16(FT2201_gammaB7.Value));

                            }
                            break;
                        case 20:
                            {
                                R6645x_Gamma_auto_Adujst(flag1, flag3, flag2, 20, 2, 1, lumg, lumr, lumb, D_lums[index, 1], D_lums[index, 0], D_lums[index, 2], flexible_gamma_count, FT2201_gammaR6.Value, FT2201_gammaG6.Value, FT2201_gammaB6.Value, 40, 32, 24, FT2201_gammaR8.Value, FT2201_gammaG8.Value, FT2201_gammaB8.Value, FT2201_gammaR7.Value, FT2201_gammaG7.Value, FT2201_gammaB7.Value, out r, out g, out b, out Gamma_adjust_timeout_flag);
                                if (Gamma_adjust_timeout_flag == true)
                                {
                                    flag1 = 0; flag3 = 0; flag2 = 0;
                                }
                                FT2201_gammaR6.Value = r;
                                FT2201_gammaG6.Value = g;
                                FT2201_gammaB6.Value = b;
                                auto_gamma_write(gamma_r[6], Convert.ToUInt16(FT2201_gammaR6.Value), gamma_g[6], Convert.ToUInt16(FT2201_gammaG6.Value), gamma_b[6], Convert.ToUInt16(FT2201_gammaB6.Value));

                            }
                            break;
                        case 21:
                            {
                                R6645x_Gamma_auto_Adujst(flag1, flag3, flag2, 10, 2, 1, lumg, lumr, lumb, D_lums[index, 1], D_lums[index, 0], D_lums[index, 2], flexible_gamma_count, FT2201_gammaR5.Value, FT2201_gammaG5.Value, FT2201_gammaB5.Value, 32, 24, 16, FT2201_gammaR7.Value, FT2201_gammaG7.Value, FT2201_gammaB7.Value, FT2201_gammaR6.Value, FT2201_gammaG6.Value, FT2201_gammaB6.Value, out r, out g, out b, out Gamma_adjust_timeout_flag);
                                if (Gamma_adjust_timeout_flag == true)
                                {
                                    flag1 = 0; flag3 = 0; flag2 = 0;
                                }
                                FT2201_gammaR5.Value = r;
                                FT2201_gammaG5.Value = g;
                                FT2201_gammaB5.Value = b;
                                auto_gamma_write(gamma_r[5], Convert.ToUInt16(FT2201_gammaR5.Value), gamma_g[5], Convert.ToUInt16(FT2201_gammaG5.Value), gamma_b[5], Convert.ToUInt16(FT2201_gammaB5.Value));

                            }
                            break;
                        case 22:
                            {
                                R6645x_Gamma_auto_Adujst(flag1, flag3, flag2, 10, 2, 1, lumg, lumr, lumb, D_lums[index, 1], D_lums[index, 0], D_lums[index, 2], flexible_gamma_count, FT2201_gammaR4.Value, FT2201_gammaG4.Value, FT2201_gammaB4.Value, 24, 16, 12, FT2201_gammaR6.Value, FT2201_gammaG6.Value, FT2201_gammaB6.Value, FT2201_gammaR5.Value, FT2201_gammaG5.Value, FT2201_gammaB5.Value, out r, out g, out b, out Gamma_adjust_timeout_flag);
                                if (Gamma_adjust_timeout_flag == true)
                                {
                                    flag1 = 0; flag3 = 0; flag2 = 0;
                                }
                                FT2201_gammaR4.Value = r;
                                FT2201_gammaG4.Value = g;
                                FT2201_gammaB4.Value = b;
                                auto_gamma_write(gamma_r[4], Convert.ToUInt16(FT2201_gammaR4.Value), gamma_g[4], Convert.ToUInt16(FT2201_gammaG4.Value), gamma_b[4], Convert.ToUInt16(FT2201_gammaB4.Value));

                            }
                            break;
                        case 23:
                            {
                                R6645x_Gamma_auto_Adujst(flag1, flag3, flag2, 5, 1, 1, lumg, lumr, lumb, D_lums[index, 1], D_lums[index, 0], D_lums[index, 2], flexible_gamma_count, FT2201_gammaR3.Value, FT2201_gammaG3.Value, FT2201_gammaB3.Value, 16, 12, 8, FT2201_gammaR5.Value, FT2201_gammaG5.Value, FT2201_gammaB5.Value, FT2201_gammaR4.Value, FT2201_gammaG4.Value, FT2201_gammaB4.Value, out r, out g, out b, out Gamma_adjust_timeout_flag);
                                if (Gamma_adjust_timeout_flag == true)
                                {
                                    flag1 = 0; flag3 = 0; flag2 = 0;
                                }
                                FT2201_gammaR3.Value = r;
                                FT2201_gammaG3.Value = g;
                                FT2201_gammaB3.Value = b;
                                auto_gamma_write(gamma_r[3], Convert.ToUInt16(FT2201_gammaR3.Value), gamma_g[3], Convert.ToUInt16(FT2201_gammaG3.Value), gamma_b[3], Convert.ToUInt16(FT2201_gammaB3.Value));

                            }
                            break;
                        case 24://阶以下
                            {
                                R6645x_Gamma_auto_Adujst(flag1, flag3, flag2, 5, 1, 1, lumg, lumr, lumb, D_lums[index, 1], D_lums[index, 0], D_lums[index, 2], flexible_gamma_count, FT2201_gammaR2.Value, FT2201_gammaG2.Value, FT2201_gammaB2.Value, 12, 8, 4, FT2201_gammaR4.Value, FT2201_gammaG4.Value, FT2201_gammaB4.Value, FT2201_gammaR3.Value, FT2201_gammaG3.Value, FT2201_gammaB3.Value, out r, out g, out b, out Gamma_adjust_timeout_flag);
                                if (Gamma_adjust_timeout_flag == true)
                                {
                                    flag1 = 0; flag3 = 0; flag2 = 0;
                                }
                                FT2201_gammaR2.Value = r;
                                FT2201_gammaG2.Value = g;
                                FT2201_gammaB2.Value = b;
                                auto_gamma_write(gamma_r[2], Convert.ToUInt16(FT2201_gammaR2.Value), gamma_g[2], Convert.ToUInt16(FT2201_gammaG2.Value), gamma_b[2], Convert.ToUInt16(FT2201_gammaB2.Value));

                            }
                            break;
                        case 25://阶以下
                            {
                                R6645x_Gamma_auto_Adujst(flag1, flag3, flag2, 5, 1, 1, lumg, lumr, lumb, D_lums[index, 1], D_lums[index, 0], D_lums[index, 2], flexible_gamma_count, FT2201_gammaR1.Value, FT2201_gammaG1.Value, FT2201_gammaB1.Value, 8, 4, 0, FT2201_gammaR3.Value, FT2201_gammaG3.Value, FT2201_gammaB3.Value, FT2201_gammaR2.Value, FT2201_gammaG2.Value, FT2201_gammaB2.Value, out r, out g, out b, out Gamma_adjust_timeout_flag);
                                if (Gamma_adjust_timeout_flag == true)
                                {
                                    flag1 = 0; flag3 = 0; flag2 = 0;
                                }
                                FT2201_gammaR1.Value = r;
                                FT2201_gammaG1.Value = g;
                                FT2201_gammaB1.Value = b;
                                auto_gamma_write(gamma_r[1], Convert.ToUInt16(FT2201_gammaR1.Value), gamma_g[1], Convert.ToUInt16(FT2201_gammaG1.Value), gamma_b[1], Convert.ToUInt16(FT2201_gammaB1.Value));

                            }
                            break;
                        case 26://阶以下
                            {
                                R6645x_Gamma_auto_Adujst(flag1, flag3, flag2, 5, 1, 1, lumg, lumr, lumb, D_lums[index, 1], D_lums[index, 0], D_lums[index, 2], flexible_gamma_count, FT2201_gammaR0.Value, FT2201_gammaG0.Value, FT2201_gammaB0.Value, 4, 0, 0, FT2201_gammaR2.Value, FT2201_gammaG2.Value, FT2201_gammaB2.Value, FT2201_gammaR1.Value, FT2201_gammaG1.Value, FT2201_gammaB1.Value, out r, out g, out b, out Gamma_adjust_timeout_flag);
                                if (Gamma_adjust_timeout_flag == true)
                                {
                                    flag1 = 0; flag3 = 0; flag2 = 0;
                                }
                                FT2201_gammaR0.Value = r;
                                FT2201_gammaG0.Value = g;
                                FT2201_gammaB0.Value = b;
                                auto_gamma_write(gamma_r[0], Convert.ToUInt16(FT2201_gammaR0.Value), gamma_g[0], Convert.ToUInt16(FT2201_gammaG0.Value), gamma_b[0], Convert.ToUInt16(FT2201_gammaB0.Value));

                            }
                            break;



                        default:
                            break;
                    }
                }
                catch
                {
                    T5.Enabled = false;

                    MessageBox.Show("数值超限！");
                }
            }
        }




        private void btnCalculate350_Click(object sender, EventArgs e)
        {
            text_gray_num0.Text = (Math.Pow((Parttern_FT2201[0] / 255.0), Convert.ToDouble(textNTGamma.Text)) * Convert.ToDouble(textNTlum.Text)).ToString("F4");
            text_gray_num1.Text = (Math.Pow((Parttern_FT2201[1] / 255.0), Convert.ToDouble(textNTGamma.Text)) * Convert.ToDouble(textNTlum.Text)).ToString("F4");
            text_gray_num2.Text = (Math.Pow((Parttern_FT2201[2] / 255.0), Convert.ToDouble(textNTGamma.Text)) * Convert.ToDouble(textNTlum.Text)).ToString("F4");
            text_gray_num3.Text = (Math.Pow((Parttern_FT2201[3] / 255.0), Convert.ToDouble(textNTGamma.Text)) * Convert.ToDouble(textNTlum.Text)).ToString("F4");
            text_gray_num4.Text = (Math.Pow((Parttern_FT2201[4] / 255.0), Convert.ToDouble(textNTGamma.Text)) * Convert.ToDouble(textNTlum.Text)).ToString("F4");
            text_gray_num5.Text = (Math.Pow((Parttern_FT2201[5] / 255.0), Convert.ToDouble(textNTGamma.Text)) * Convert.ToDouble(textNTlum.Text)).ToString("F4");
            text_gray_num6.Text = (Math.Pow((Parttern_FT2201[6] / 255.0), Convert.ToDouble(textNTGamma.Text)) * Convert.ToDouble(textNTlum.Text)).ToString("F4");
            text_gray_num7.Text = (Math.Pow((Parttern_FT2201[7] / 255.0), Convert.ToDouble(textNTGamma.Text)) * Convert.ToDouble(textNTlum.Text)).ToString("F4");
            text_gray_num8.Text = (Math.Pow((Parttern_FT2201[8] / 255.0), Convert.ToDouble(textNTGamma.Text)) * Convert.ToDouble(textNTlum.Text)).ToString("F4");
            text_gray_num9.Text = (Math.Pow((Parttern_FT2201[9] / 255.0), Convert.ToDouble(textNTGamma.Text)) * Convert.ToDouble(textNTlum.Text)).ToString("F4");
            text_gray_num10.Text = (Math.Pow((Parttern_FT2201[10] / 255.0), Convert.ToDouble(textNTGamma.Text)) * Convert.ToDouble(textNTlum.Text)).ToString("F4");
            text_gray_num11.Text = (Math.Pow((Parttern_FT2201[11] / 255.0), Convert.ToDouble(textNTGamma.Text)) * Convert.ToDouble(textNTlum.Text)).ToString("F4");
            text_gray_num12.Text = (Math.Pow((Parttern_FT2201[12] / 255.0), Convert.ToDouble(textNTGamma.Text)) * Convert.ToDouble(textNTlum.Text)).ToString("F4");
            text_gray_num13.Text = (Math.Pow((Parttern_FT2201[13] / 255.0), Convert.ToDouble(textNTGamma.Text)) * Convert.ToDouble(textNTlum.Text)).ToString("F4");
            text_gray_num14.Text = (Math.Pow((Parttern_FT2201[14] / 255.0), Convert.ToDouble(textNTGamma.Text)) * Convert.ToDouble(textNTlum.Text)).ToString("F4");
            text_gray_num15.Text = (Math.Pow((Parttern_FT2201[15] / 255.0), Convert.ToDouble(textNTGamma.Text)) * Convert.ToDouble(textNTlum.Text)).ToString("F4");
            text_gray_num16.Text = (Math.Pow((Parttern_FT2201[16] / 255.0), Convert.ToDouble(textNTGamma.Text)) * Convert.ToDouble(textNTlum.Text)).ToString("F4");
            text_gray_num17.Text = (Math.Pow((Parttern_FT2201[17] / 255.0), Convert.ToDouble(textNTGamma.Text)) * Convert.ToDouble(textNTlum.Text)).ToString("F4");
            text_gray_num18.Text = (Math.Pow((Parttern_FT2201[18] / 255.0), Convert.ToDouble(textNTGamma.Text)) * Convert.ToDouble(textNTlum.Text)).ToString("F4");
            text_gray_num19.Text = (Math.Pow((Parttern_FT2201[19] / 255.0), Convert.ToDouble(textNTGamma.Text)) * Convert.ToDouble(textNTlum.Text)).ToString("F4");
            text_gray_num20.Text = (Math.Pow((Parttern_FT2201[20] / 255.0), Convert.ToDouble(textNTGamma.Text)) * Convert.ToDouble(textNTlum.Text)).ToString("F4");
            text_gray_num21.Text = (Math.Pow((Parttern_FT2201[21] / 255.0), Convert.ToDouble(textNTGamma.Text)) * Convert.ToDouble(textNTlum.Text)).ToString("F4");
            text_gray_num22.Text = (Math.Pow((Parttern_FT2201[22] / 255.0), Convert.ToDouble(textNTGamma.Text)) * Convert.ToDouble(textNTlum.Text)).ToString("F4");
            text_gray_num23.Text = (Math.Pow((Parttern_FT2201[23] / 255.0), Convert.ToDouble(textNTGamma.Text)) * Convert.ToDouble(textNTlum.Text)).ToString("F4");
            text_gray_num24.Text = (Math.Pow((Parttern_FT2201[24] / 255.0), Convert.ToDouble(textNTGamma.Text)) * Convert.ToDouble(textNTlum.Text)).ToString("F4");
            text_gray_num25.Text = (Math.Pow((Parttern_FT2201[25] / 255.0), Convert.ToDouble(textNTGamma.Text)) * Convert.ToDouble(textNTlum.Text)).ToString("F4");
            text_gray_num26.Text = (Math.Pow((Parttern_FT2201[26] / 255.0), Convert.ToDouble(textNTGamma.Text)) * Convert.ToDouble(textNTlum.Text)).ToString("F4");
            ///4灰阶设定为8灰阶的一半
            //text_gray_num25.Text = (Convert.ToDouble(text_gray_num24.Text) / 2).ToString("F4");
            //////0灰阶更换为对比度典型值计算。
            //text_gray_num26.Text = (Convert.ToDouble(textNTlum.Text)/ Convert.ToDouble(textBox_CR.Text)).ToString("F4");
        }




    }
}
